1177. Wildcard Mirror Check
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
?, andadiffers fromc, 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)