Mirror Phrase Contest

Easytwo pointersAlso known as Valid Palindrome

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

Example 1
Input
entry = "Top spot."
Output
true
Why

Letters and digits, lowercased: topspot. Backwards it is still topspot, so it passes.

Example 2
Input
entry = "Step on pets"
Output
false
Why

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.

Example 3
Input
entry = "0P"
Output
false
Why

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

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.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code