1177. Wildcard Mirror Check

Easy1.5 points estimatedTwo PointersString

A word-game app lets players leave blank tiles when they build a mirror word, one that reads the same forwards and backwards. The board is the string s, made of lowercase letters and the wildcard '?', where each '?' may be replaced by any single lowercase letter, chosen independently of the others.

Return true if some choice of replacements turns s into a palindrome, and false otherwise. Compare characters from both ends moving inward with two pointers: a pair is fine when the two letters are equal or at least one of them is '?'. The middle character of an odd-length string never conflicts with anything.

This runs in linear time with constant extra space.

Example 1

Input:
s = "ab?ba"
Output:
true
Explanation:

The outer pairs match and the middle character is free, so the string can be a palindrome.

Example 2

Input:
s = "a?c"
Output:
false
Explanation:

The first and last letters differ and neither is ?, and a differs from c, so no choice of replacement can make a palindrome.

Example 3

Input:
s = "??"
Output:
true
Explanation:

Both characters are wildcards, so they can both become the same letter.

Constraints

1 ≤ s.length ≤ 105

s contains lowercase letters and '?' only

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…