Mirror Check Ignoring Punctuation and Letter Case
StringsAuto-graded · 24 test casesLast updated
Check whether a string is a palindrome when only letters and digits count and case is ignored, using two pointers with constant extra space.
You are given a string text. Consider only its alphanumeric characters (letters and digits), and treat uppercase and lowercase versions of a letter as the same character. Every other character (spaces, punctuation, symbols, underscores) is ignored.
Return True if the remaining characters, read in order, are identical to the same characters read in reverse. Otherwise return False.
Details:
- If no alphanumeric characters remain (for example, an empty string or a string of only spaces and punctuation), the answer is
True. - Digits count as real characters and must match like any other character.
- Aim for O(n) time and O(1) extra space. Do not build a filtered or reversed copy of the string; compare characters in place by moving two indices toward each other.
Constraints
0 <= len(text) <= 2 * 10^5textconsists of printable ASCII characters.- Return a boolean.
Examples
Example 1
Input: is_mirror_text('A man, a plan, a canal: Panama')
Output: True
Example 2
Input: is_mirror_text('race a car')
Output: False
Example 3
Input: is_mirror_text(' ')
Output: True
Example 4
Input: is_mirror_text('No \'x\' in Nixon')
Output: True
Hints
Hint 1
Building a cleaned copy and comparing it to its reverse works, but it uses O(n) extra memory. Try to check the original string directly.
Hint 2
Place one index at the start and one at the end. Move each one inward until it points at an alphanumeric character, then compare the two characters ignoring case.
Hint 3
Keep the `left < right` condition in the skipping loops so the indices never cross or leave the string when a side contains only punctuation. Remember that digits are significant.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A simple solution creates a cleaned, lowercased copy of the string and compares it to its reverse. That is O(n) time but also O(n) extra space, so we avoid it.
Instead, use two indices, left at the start and right at the end:
- Move
leftforward while it points at a non-alphanumeric character. - Move
rightbackward while it points at a non-alphanumeric character. - Compare the two characters after lowercasing. If they differ, return
False. - Step both indices inward and repeat while
left < right. - If the loop ends without a mismatch, return
True.
Why it is correct
Let t be the cleaned string (alphanumerics only, lowercased). The input qualifies exactly when t[i] == t[len(t) - 1 - i] for every i. At the start of each outer iteration, all alphanumeric characters left of left have been matched with their mirror partners right of right. The inner loops move each index to the next unmatched alphanumeric character, which is precisely the next mirror pair of t. A mismatch proves the string is not valid; finishing the loop means every pair matched. A single middle character needs no partner.
The left < right guard in the inner loops prevents the indices from running past each other or out of bounds. For input like ".,", left stops at right, the code compares a character to itself, and it correctly returns True because the cleaned string is empty.
Complexity
- Time: O(n). Each index only moves inward, so the total movement is at most n, with O(1) work per step.
- Space: O(1) extra. Only two integers are stored (a one-character lowercase string is constant size).
Reference solution
def is_mirror_text(text: str) -> bool:
left, right = 0, len(text) - 1
while left < right:
while left < right and not text[left].isalnum():
left += 1
while left < right and not text[right].isalnum():
right -= 1
if text[left].lower() != text[right].lower():
return False
left += 1
right -= 1
return True