forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Update 125-Valid-Palindrome.py"
- Loading branch information
1 parent
b78ea5a
commit 857360f
Showing
1 changed file
with
15 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
l = 0 | ||
r = len(s)-1 | ||
while l<r: | ||
if s[l].lower()==s[r].lower(): | ||
l+=1 | ||
r-=1 | ||
continue | ||
|
||
elif not (65<=ord(s[l])<=90 or 97<=ord(s[l])<=122 or 48<=ord(s[l])<=57): | ||
l+=1 | ||
elif not (65<=ord(s[r])<=90 or 97<=ord(s[r])<=122 or 48<=ord(s[r])<=57): | ||
r-=1 | ||
else: | ||
l, r = 0, len(s) - 1 | ||
while l < r: | ||
while l < r and not self.alphanum(s[l]): | ||
l += 1 | ||
while l < r and not self.alphanum(s[r]): | ||
r -= 1 | ||
if s[l].lower() != s[r].lower(): | ||
return False | ||
l += 1 | ||
r -= 1 | ||
return True | ||
|
||
# Could write own alpha-numeric function | ||
def alphanum(self, c): | ||
return (ord('A') <= ord(c) <= ord('Z') or | ||
ord('a') <= ord(c) <= ord('z') or | ||
ord('0') <= ord(c) <= ord('9')) |