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
'Problem Solving' 카테고리의 다른 글
[Leetcode] 242. Valid Anagram (0) | 2023.07.13 |
---|---|
[Leetcode] 226. Invert Binary Tree (0) | 2023.07.13 |
[Leetcode] 121. Best Time to Buy and Sell Stock (0) | 2023.07.12 |
[Leetcode] 20. Valid Parentheses (0) | 2023.07.11 |
[Leetcode] 167. Two Sum II - Input Array Is Sorted (0) | 2023.07.11 |