-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0125_valid_palindrome.py
More file actions
96 lines (73 loc) · 2.58 KB
/
0125_valid_palindrome.py
File metadata and controls
96 lines (73 loc) · 2.58 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#------------------------------------------------------------------------------
# Question:
#------------------------------------------------------------------------------
# tags:
'''
125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric
characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
'''
#------------------------------------------------------------------------------
# Solutions
#------------------------------------------------------------------------------
from typing import *
class Solution:
'''
Time: O(n)
Space: O(n)
Intuition: add the first half of chars to a stack then compare the rest
with the top of the stack. For odd numbers the index needs to be increased.
'''
def isPalindrome(self, s: str) -> bool:
new_s = ''.join(c.lower() for c in s if c.isalpha() or c.isdigit())
n = len(new_s)
mid = n // 2
stack = []
for i in range(mid):
stack.append(new_s[i])
#for odd strings
mid = mid + 1 if n % 2 == 1 else mid
for i in range(mid, n):
top = stack.pop()
if top != new_s[i]:
return False
return True
class Solution2:
def isPalindrome(self, s: str) -> bool:
new_s = ''.join(c.lower() for c in s if c.isalpha() or c.isdigit())
i = 0
start = 0
end = len(new_s) - 1
print(f'Testing:s[{start}:{end}]:{s[start:end]}')
return all([s[k] == s[end-k+start] for k in range(start,end+1)])
#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------
import unittest
class TestSolution(unittest.TestCase):
def test_simple(self):
string = "A man, a plan, a canal: Panama"
s = Solution()
self.assertEqual(s.isPalindrome(string), True)
s = Solution2()
self.assertEqual(s.isPalindrome(string), False)
def test_simple2(self):
string = "0P"
s = Solution()
self.assertEqual(s.isPalindrome(string), False)
s = Solution2()
self.assertEqual(s.isPalindrome(string), False)
def test_simple3(self):
string = "aa"
s = Solution()
self.assertEqual(s.isPalindrome(string), True)
s = Solution2()
self.assertEqual(s.isPalindrome(string), True)
unittest.main(verbosity=2)