-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_valid-palindrome.py
57 lines (47 loc) · 1.54 KB
/
leetcode_valid-palindrome.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# https://leetcode.com/problems/valid-palindrome/
# 0 번째 외의 방법은 전부 "파이썬 알고리즘 인터뷰"를 참조함
# method 0 : Using String : 40 ms 14.4 MB
class Solution:
def isPalindrome(self, s: str) -> bool:
# refine unnecessary words
clean_s = ""
for char in s:
if char.isalnum():
clean_s += char
clean_s = clean_s.lower()
len_s = len(clean_s)
if clean_s == "":
return True
# we don't consider whether the number is even or odd.
for i in range(int(len_s / 2)):
if clean_s[i] != clean_s[-i - 1]:
return False
return True
# method 1: Using List : 284 ms 19.4 MB
class Solution:
def isPalindrome(self, s: str) -> bool:
strs=[]
for char in s:
if char.isalnum():
strs.append(char.lower())
while len(strs)>1:
if strs.pop(0)!=strs.pop():
return False
return True
# method 2 : Using Deque : 48 ms 19.4 MB
class Solution:
def isPalindrome(self, s: str) -> bool:
strs : Deque = collections.deque()
for char in s:
if char.isalnum():
strs.append(char.lower())
while len(strs)>1:
if strs.popleft()!=strs.pop():
return False
return True
# method 3 : Using Slicing : 36 ms 15.8 MB
class Solution:
def isPalindrome(self, s: str) -> bool:
s=s.lower()
s=re.sub('[^a-z0-9]','',s)
return s == s[::-1] # Slicing