File tree Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def isPalindrome (self , s : str ) -> bool :
3
- l = 0
4
- r = len (s )- 1
5
- while l < r :
6
- if s [l ].lower ()== s [r ].lower ():
7
- l += 1
8
- r -= 1
9
- continue
10
-
11
- elif not (65 <= ord (s [l ])<= 90 or 97 <= ord (s [l ])<= 122 or 48 <= ord (s [l ])<= 57 ):
12
- l += 1
13
- elif not (65 <= ord (s [r ])<= 90 or 97 <= ord (s [r ])<= 122 or 48 <= ord (s [r ])<= 57 ):
14
- r -= 1
15
- else :
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 ():
16
10
return False
11
+ l += 1
12
+ r -= 1
17
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' ))
You can’t perform that action at this time.
0 commit comments