Skip to content

Commit 7b9ffde

Browse files
authored
Create 125-Valid-Palindrome.py
1 parent c6410bb commit 7b9ffde

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

125-Valid-Palindrome.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
l, r = 0, len(s) - 1
4+
while l < r:
5+
while l < r and not self.alphanum(s[l]):
6+
l += 1
7+
while l < r and not self.alphanum(s[r]):
8+
r -= 1
9+
if s[l].lower() != s[r].lower():
10+
return False
11+
l += 1
12+
r -= 1
13+
return True
14+
15+
# Could write own alpha-numeric function
16+
def alphanum(self, c):
17+
return (ord('A') <= ord(c) <= ord('Z') or
18+
ord('a') <= ord(c) <= ord('z') or
19+
ord('0') <= ord(c) <= ord('9'))

0 commit comments

Comments
 (0)