-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125_Valid_Palindrome.py
More file actions
86 lines (70 loc) · 2.4 KB
/
125_Valid_Palindrome.py
File metadata and controls
86 lines (70 loc) · 2.4 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
'''
125. Valid Palindrome
Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
'''
# Brute Force
# Time Complexity: O(n)
# Space Complexity: O(n)
'''
Below is the code for the brute force approach.
It uses the built-in string methods to clean the input string and check if it is a palindrome.
'''
class Solution:
def isPalindrome(self, s):
clean = ''.join(c for c in s if c.isalnum()).lower()
return clean == clean[::-1]
# Two Pointers
# Time Complexity: O(n)
# Space Complexity: O(n)
'''
Below is the code for the two pointers approach.
It uses two pointers to check if the cleaned string is a palindrome.
The two pointers start at the beginning and end of the string and move towards the center, comparing characters along the way.
If any characters don't match, it returns False. If all characters match, it returns True.
'''
class Solution:
def isPalindrome(self, s):
clean = ''.join(c for c in s if c.isalnum()).lower()
i=0
j=len(clean)-1
while i<=j:
if clean[i]!=clean[j]:
return False
else:
i+=1
j-=1
return True
# Two Pointers (Optimized)
# Time Complexity: O(n)
# Space Complexity: O(1)
'''
This approach uses the two-pointer technique to efficiently check if a string is a palindrome
while ignoring non-alphanumeric characters and treating uppercase and lowercase letters as the same
'''
class Solution:
def isPalindrome(self, s):
i=0
j=len(s)-1
while i<j:
while i<j and not s[i].isalnum():
i+=1
while i<j and not s[j].isalnum():
j-=1
if s[i].lower()!=s[j].lower():
return False
i+=1
j-=1
return True
sol = Solution()
s = "Was it a car or a cat I saw?"
print(sol.isPalindrome(s))
# Output: True