-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-Solution-N125.py
More file actions
56 lines (44 loc) · 2.01 KB
/
LeetCode-Solution-N125.py
File metadata and controls
56 lines (44 loc) · 2.01 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
class Solution:
def isPalindromeTwoPointer(self, s: str) -> bool:
# Step 1: Initialize an empty string to hold the filtered characters
filtered_s = ""
# Step 2: Filter out non-alphanumeric characters and convert to lowercase
for letter in s:
if letter.isalpha() or letter.isdigit():
if letter.isupper():
filtered_s += letter.lower()
else:
filtered_s += letter
# Step 3: Use two pointers to check if the filtered string is a palindrome
left = 0
right = len(filtered_s) - 1
while left < right:
# If characters at the two pointers are not equal, return False
if filtered_s[left] != filtered_s[right]:
return False
break
left += 1
right -= 1
# Step 4: If we reach here, it means the string is a palindrome
return True
# Time Complexity: O(n)
# Space Complexity: O(n) for the filtered string
def isPalindrome(self, s: str) -> bool:
# Step 1: Initialize an empty string to hold the filtered characters
filtered_s = ""
# Step 2: Filter out non-alphanumeric characters and convert to lowercase
for letter in s:
if letter.isalpha() or letter.isdigit():
if letter.isupper():
filtered_s += letter.lower()
else:
filtered_s += letter
# Step 3: Check if the filtered string is equal to its reverse
return filtered_s == filtered_s[::-1]
# Time Complexity: O(n)
# Space Complexity: O(n) for the filtered string
s = "A man, a plan, a canal: Panama"
s1 = "race a car"
solution = Solution()
print(solution.isPalindromeTwoPointer(s)) # Output: True
print(solution.isPalindrome(s1)) # Output: False