-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125.valid-palindrome.py
More file actions
47 lines (38 loc) · 1.09 KB
/
125.valid-palindrome.py
File metadata and controls
47 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Nonoptimal solution
class Solution_one:
def isPalindrome(self, s: str) -> bool:
start = 0
end = len(s) - 1
while start < end:
while start < end:
if s[start].lower().isalnum():
break
start += 1
while end > start:
if s[end].lower().isalnum():
break
end -= 1
if s[start].lower() != s[end].lower():
if not s[start].isalnum() or not s[end].isalnum():
return True
return False
start += 1
end -= 1
return True
# @leet start
class Solution:
def isPalindrome(self, s: str) -> bool:
p1, p2 = 0, len(s) - 1
while p1 < p2:
if not s[p1].isalnum():
p1 += 1
continue
if not s[p2].isalnum():
p2 -= 1
continue
if s[p1].lower() != s[p2].lower():
return False
p1 += 1
p2 -= 1
return True
# @leet end