Problem Solving
[Leetcode] 125. Valid Palindrome
leeyngdo
2023. 7. 12. 17:12
Problem Link: https://leetcode.com/problems/valid-palindrome/
Valid Palindrome - LeetCode
Can you solve this real interview question? Valid Palindrome - A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric cha
leetcode.com
Solution 1. Two Pointers
Time: $O(N)$
Space: $O(1)$
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
start = 0
end = len(s) - 1
while start < end:
if not s[start].isalnum():
start += 1
continue
elif not s[end].isalnum():
end -= 1
continue
elif s[start].lower() != s[end].lower():
return False
start += 1; end -= 1
return True