-
Notifications
You must be signed in to change notification settings - Fork 0
/
125ValidPalindrome.py
executable file
·41 lines (40 loc) · 1.13 KB
/
125ValidPalindrome.py
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
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
m_len = len(s)
if s == "" or m_len == 1or s.isspace():
return True
i = 0
j = m_len - 1
bRel = True
while i < j:
while (not s[i].isalnum()) and i < j:
i += 1
if i == j:
break
while (not s[j].isalnum()) and j > i:
j -= 1
if j == i:
break
if s[i].isalpha() and s[j].isalpha():
if s[i].lower() != s[j].lower():
bRel = False
break
elif s[i].isdigit() and s[j].isdigit():
if s[i] != s[j]:
bRel = False
break
else:
bRel = False
break
j -= 1
i += 1
return bRel
c = Solution()
print(c.isPalindrome("A man, a plan, a canal: Panama"))
print(c.isPalindrome(" "))
print(c.isPalindrome(".,"))
print(c.isPalindrome("\"Sue,\" Tom smiles \"Selim smote us.\""))