Mirror Phrase Contest
A word-puzzle club runs a contest for phrases that read the same forwards and backwards, like "Top spot." or "12:21". The judges look only at letters and digits, ignoring spaces and punctuation, and capitals count the same as small letters.
So "Top spot." is judged as topspot, which mirrors perfectly. With thousands of entries, the club wants a program for the first round.
Your task
Write isMirrorPhrase(entry). Return true if the letters and digits of entry read the same left to right as right to left, ignoring case. Otherwise return false.
Every character that is not a letter (a-z, A-Z) or digit (0-9) is ignored. Digits must mirror like letters. An entry with no letters or digits counts as a mirror phrase.
isMirrorPhrase(entry: string) → bool
Examples
entry = "Top spot."
true
Letters and digits, lowercased: topspot. Backwards it is still topspot, so it passes.
entry = "Step on pets"
false
The letters are steponpets; backwards, stepnopets. The ends match (s and s, t and t, e and e, p and p) until o meets n, so it fails.
entry = "0P"
false
Digits count. The characters are 0 and p, which differ. Throwing digits away would leave just p and wrongly answer true.
Constraints
- 0 ≤ entry.length ≤ 2,000
- entry contains printable ASCII characters only (letters, digits, spaces, punctuation).
Start with brute force
Build a cleaned lowercase copy of just the letters and digits, reverse it, and compare.
That is correct and O(n), fine at this size, but it builds two extra strings as long as the input. Checking from both ends of the original needs no copies and stops at the first mismatch.
Hints
0 of 4 shown- Hint 1
Take
"Top spot.". Which character must the firstTmatch, where is it, and what did you skip to reach it? - Hint 2
Check from both ends at once: the first counted character against the last, the second against the second last, and so on.
- Hint 3
Put one finger at the start and one at the end. If a finger is on a space or mark, move just that finger inward and look again.
- Hint 4
Use
leftandrightindexes. Skip characters that are not letters or digits. When both point at real characters, compare them in lowercase: if they differ, return false; if they match, move both inward. Stop when they meet.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
A mirror phrase pairs its first counted character with its last, its second with its second last, and so on. So check those pairs directly from both ends of the original string, skipping anything that does not count.
Trace "Top spot." (indexes 0 to 8):
left= 0 (T),right= 8 (.). The period does not count. Moverightto 7.Tandt: equal in lowercase.left= 1,right= 6.oando: equal.left= 2,right= 5.pandp: equal.left= 3,right= 4.leftis on the space. Skip it:left= 4 =right, so the loop ends. Every pair matched: return true.
The middle character needs no partner, so the loop stops when the indexes meet.
function isMirrorPhrase(entry) {
const counts = (ch) => /[a-z0-9]/i.test(ch);
let left = 0, right = entry.length - 1;
while (left < right) {
if (!counts(entry[left])) { left++; continue; }
if (!counts(entry[right])) { right--; continue; }
if (entry[left].toLowerCase() !== entry[right].toLowerCase()) return false;
left++;
right--;
}
return true;
}def is_mirror_phrase(entry):
left, right = 0, len(entry) - 1
while left < right:
if not entry[left].isalnum():
left += 1
elif not entry[right].isalnum():
right -= 1
elif entry[left].lower() != entry[right].lower():
return False
else:
left += 1
right -= 1
return TrueRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.