Skip to content

Commit 810bbf6

Browse files
committed
125. Valid Palindrome Solution
1 parent 19e94c7 commit 810bbf6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

valid-palindrome/doh6077.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import re
2+
# two pointers
3+
class Solution:
4+
def isPalindrome(self, s: str) -> bool:
5+
# remove all alphanumeric characters and convert all uppercase letters into lowercase letters
6+
cleaned_s = re.sub(r'[^a-zA-Z0-9]', '', s).lower()
7+
n = len(cleaned_s)
8+
left, right = 0, n -1
9+
while left < right:
10+
if cleaned_s[left] == cleaned_s[right]:
11+
left += 1
12+
right -= 1
13+
else:
14+
return False
15+
16+
return True

0 commit comments

Comments
 (0)