CodeArchon

easyTwo PointersMicrosoftFacebookAmazon

Mirror Sign Verifier

A billboard installer tests whether a slogan reads the same from both ends after stripping punctuation and casing.

Given a string `text`, decide whether it is a palindrome after converting all letters to lowercase and removing non-alphanumeric characters.

Choose approach

a
0
m
1
a
2
n
3
a
4
p
5
l
6
a
7
n
8
a
9
c
10
a
11
n
12
a
13
l
14
p
15
a
16
n
17
a
18
m
19
a
20
1/23
Narrationstep 1 of 23

Cleaned "A man, a plan, a canal: Panama" to lowercase alphanumerics: "amanaplanacanalpanama". Set two pointers at the ends.

1def is_mirror_sign(text: str) -> bool:
2 chars = [c.lower() for c in text if c.isalnum()]
3 lo, hi = 0, len(chars) - 1
4 while lo < hi:
5 if chars[lo] != chars[hi]:
6 return False
7 lo += 1
8 hi -= 1
9 return True
Variables
cleaned=amanaplanacanalpanama
lo=—
hi=—

How to think about it

  1. Strip the input to lowercase alphanumerics first — that's what the question actually asks about.
  2. Palindrome check = compare mirrored positions. Natural fit for two pointers.
  3. lo starts at 0, hi at length−1; step them toward each other.
  4. First mismatch → false. Pointers crossing → true.
  5. Alternative without cleaning: skip non-alphanumerics inline while walking.

Edge cases to catch in interview

  • Empty string → vacuously true.
  • All non-alphanumerics → cleans to empty → true.
  • Single character → true.
  • Case: 'Aa' → 'aa' → true.

Examples

Example 1true
{"text":"A man, a plan, a canal: Panama","values":["a","m","a","n","a","p","l","a","n","a","c","a","n","a","l","p","a","n","a","m","a"]}

'amanaplanacanalpanama' reads the same both ways.

Example 2false
{"text":"race a car","values":["r","a","c","e","a","c","a","r"]}

'raceacar' is not a palindrome.

Example 3true
{"text":" ","values":[]}

Cleans to the empty string.

Try your own input

SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed