Topic: Arrays & Hashing
Soln 1 (isalnum):
A not so efficient solution:
- create a new list to store the palindrome
- loop through the characters in the string:
- check if it is alphanumeric, if it is then append to list
- assign a variable, then convert palindrome list to string
- assign another variable, then reverse palindrome
- return and compare both variables
def isPalindrome(self, s: str) -> bool:
palin = []
for c in s:
if c.isalnum():
palin.append(c.lower())
x = ''.join(palin)
y = x[::-1]
return x == y
Soln 2 (isalnum):
Filter and Normalize: Create palindrome by joining all alphanumeric characters from s and converting them to lowercase.
Check Palindrome: Return True if palindrome is equal to its reverse, otherwise return False.
def isPalindrome(self, s: str) -> bool:
palindrome = ''.join(filter(str.isalnum, s)).lower()
return palindrome == palindrome[::-1]
Notes: Attempt to understand 2 pointer solution.
Top comments (0)