Ctrl K

Mirror Phrase Check

Easy
Call a phrase a mirror phrase if it reads the same left to right and right to left, looking only at letters and digits and ignoring letter case. Spaces, punctuation and any other characters are skipped. Given a string text, return True if it is a mirror phrase and False otherwise. Try to do it without building a copy of the string, in O(1) extra memory.
Example 1
text="Step on no pets!" → True

Keeping only the letters gives "steponnopets", which is the same in both directions.

Example 2
text="Taco cat, 2 go" → False

After cleaning we get "tacocat2go". It starts with t but ends with o, so it is not a mirror.

Example 3
text="?! .." → True

No letters or digits remain, and an empty string reads the same both ways.

Constraints
  • 1 ≤ len(text) ≤ 2 · 10⁵
  • text consists of printable ASCII characters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsWhere the Fragment Starts EasySame When Flipped EasyFlip the Characters in Place EasyMirror the Vowels EasyHidden in Order Easy
loading Python…
Input
text="Step on no pets!"
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 5 tests on the server, hidden ones included.