Skip to content

Commit 2497f0b

Browse files
Add No_125: Valid Palindrome
1 parent c308827 commit 2497f0b

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
3+
Description:
4+
5+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
6+
7+
Note: For the purpose of this problem, we define empty string as valid palindrome.
8+
9+
Example 1:
10+
11+
Input: "A man, a plan, a canal: Panama"
12+
Output: true
13+
Example 2:
14+
15+
Input: "race a car"
16+
Output: false
17+
18+
'''
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'''
2+
3+
Description:
4+
5+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
6+
7+
Note: For the purpose of this problem, we define empty string as valid palindrome.
8+
9+
Example 1:
10+
11+
Input: "A man, a plan, a canal: Panama"
12+
Output: true
13+
Example 2:
14+
15+
Input: "race a car"
16+
Output: false
17+
18+
'''
19+
20+
21+
import re
22+
23+
class Solution:
24+
25+
def refine(self, s:str) ->str:
26+
27+
regex = r'[^A-Za-z0-9]'
28+
pattern = re.compile(regex)
29+
30+
return pattern.sub('', s)
31+
32+
def isPalindrome(self, s: str) -> bool:
33+
34+
alphanumeric_s = self.refine(s).lower()
35+
36+
return alphanumeric_s == alphanumeric_s[::-1]
37+
38+
39+
40+
# n : the length of inputs string s
41+
42+
## Time Complexity: O( n )
43+
#
44+
# The major overhead in time is the refine function and [::-1] string copy, which is of O( n ).
45+
46+
47+
## Space Complexity: O( n )
48+
#
49+
# The compare is out-of-place, need extra space of O( n )
50+
# The major overhead in space is storage for out-of-place comparison, which is of O( n )
51+
52+
53+
54+
def test_bench():
55+
56+
test_data = [
57+
"A man, a plan, a canal: Panama",
58+
"race a car"
59+
]
60+
61+
# expected output:
62+
'''
63+
True
64+
False
65+
'''
66+
67+
for test_s in test_data:
68+
69+
print( Solution().isPalindrome(test_s) )
70+
71+
return
72+
73+
74+
75+
if __name__ == '__main__':
76+
77+
test_bench()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'''
2+
3+
Description:
4+
5+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
6+
7+
Note: For the purpose of this problem, we define empty string as valid palindrome.
8+
9+
Example 1:
10+
11+
Input: "A man, a plan, a canal: Panama"
12+
Output: true
13+
Example 2:
14+
15+
Input: "race a car"
16+
Output: false
17+
18+
'''
19+
20+
class Solution:
21+
22+
23+
def isPalindrome(self, s: str) -> bool:
24+
25+
left, right = 0, len(s)-1
26+
27+
while left < right:
28+
29+
# skip non-alphanumeric characters on left hand side
30+
while not s[left].isalnum() and left < right:
31+
left += 1
32+
33+
# skip non-alphanumeric characters on right hand side
34+
while not s[right].isalnum() and left <right:
35+
right -=1
36+
37+
# compare
38+
if s[left].lower() != s[right].lower():
39+
return False
40+
41+
left += 1
42+
right -= 1
43+
44+
45+
return True
46+
47+
48+
49+
# n : the length of inputs string s
50+
51+
## Time Complexity: O( n )
52+
#
53+
# The major overhead in time is the while loop iterating on (left < right), which is of O( n ).
54+
55+
56+
## Space Complexity: O( 1 )
57+
#
58+
# The compare is in-place, no need of extra space to copy string
59+
# The major overhead in space is two-pointers (i.e., left and right), which is of O( 1 ).
60+
61+
62+
63+
def test_bench():
64+
65+
test_data = [
66+
"A man, a plan, a canal: Panama",
67+
"race a car"
68+
]
69+
70+
# expected output:
71+
'''
72+
True
73+
False
74+
'''
75+
76+
for test_s in test_data:
77+
78+
print( Solution().isPalindrome(test_s) )
79+
80+
return
81+
82+
83+
84+
if __name__ == '__main__':
85+
86+
test_bench()

0 commit comments

Comments
 (0)