Valid Palindrome

Easy#125towards each other

You get a string. Find out whether it reads the same left to right and right to left once you keep only the letters and digits and ignore upper and lower case. Our example: "Taco, cat!" — yes, it is a palindrome.

125 Valid PalindromeWalkthrough · 1×

Example: "Taco, cat!". The pointers walk in from both ends and step over anything that is not a letter or a digit.

0T1a2c3o4,5␣6c7a8t9!left
not checked yetunder a pointermatchedskipped

left starts at the first character, "T"

01/34
restart · back · play · step
Speed
1function isPalindrome(s):
2 left = 0
3 right = len(s) - 1
4 while left < right:
5 if not isLetterOrDigit(s[left]):
6 left = left + 1
7 else if not isLetterOrDigit(s[right]):
8 right = right - 1
9 else if lower(s[left]) != lower(s[right]):
10 return false
11 else:
12 left = left + 1; right = right - 1
13 return true
state
left
0
right
–
s[left]
T
s[right]
–
historyclick a step to go back

← → step · space play/pause

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.