philosyang.com

125. Valid Palindrome

The more meaningful takeaway from this question for me is a refresher on common String class methods in Python.

 1>>> 'a'.isalnum()  # !
 2True
 3>>> 'a'.isalpha()
 4True
 5>>> 'a'.isnumeric()
 6False
 7>>> 'a'.lower()
 8'a'
 9>>> '1'.lower()  # !
10'1'
11>>> '1'.upper()
12'1'

Two pointers (O(1) space) example solution:

 1class Solution:
 2    def isPalindrome(self, s: str) -> bool:
 3        l, r = 0, len(s) - 1
 4
 5        while l < r:
 6            a, b = s[l], s[r]
 7
 8            if not a.isalnum():
 9                l += 1
10                continue
11            elif not b.isalnum():
12                r -= 1
13                continue
14
15            if a.lower() != b.lower():
16                return False
17
18            l += 1
19            r -= 1
20
21        return True

#Neetcode150 #Two-Pointers #Python