-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid Palindrome.py
More file actions
43 lines (39 loc) · 1.06 KB
/
Copy pathValid Palindrome.py
File metadata and controls
43 lines (39 loc) · 1.06 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
import math
__author__ = 'Martin'
'''
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
str = ""
for c in s:
if c >= 'a' and c <= 'z' or c >= 'A' and c <='Z' or c >= '0' and c <= '9':
str += c.lower()
length = len(str)
for i in range(int(math.floor(length / 2))):
if str[i] != str[len(str)-1-i]:
return False
return True
'''
class Solution(object):
def isPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
end = len(s) - 1
start = 0
while start < end:
while start < end and not s[start].isalnum():
start += 1
while start < end and not s[end].isalnum():
end -= 1
if start < end and s[start].lower() != s[end].lower():
return False
start += 1
end -= 1
return True
s = Solution()
print(s.isPalindrome("A man, a plan, a canal: Panama"))