Skip to content

Commit 9b357f4

Browse files
added two pointer directory and added solution to is_palindrome
1 parent 729c1f9 commit 9b357f4

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

data_structures/two_pointer/__init__.py

Whitespace-only changes.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
Problem Overview:
3+
4+
Is Palindrome - Easy - Spotify
5+
6+
Given a string s, return true if it is a palindrome, otherwise return false.
7+
8+
A palindrome is a string that reads the same forward and backward.
9+
It is also case-insensitive and ignores all non-alphanumeric characters.
10+
11+
Example 1:
12+
13+
Input: s = "Was it a car or a cat I saw?"
14+
15+
Output: true
16+
Explanation: After considering only alphanumerical characters
17+
we have "wasitacaroracatisaw", which is a palindrome.
18+
19+
Example 2:
20+
21+
Input: s = "tab a cat"
22+
23+
Output: false
24+
Explanation: "tabacat" is not a palindrome.
25+
26+
Constraints:
27+
28+
1 <= s.length <= 1000
29+
s is made up of only printable ASCII characters.
30+
31+
"""
32+
33+
34+
class Solution:
35+
def is_palindrome(self, s: str) -> bool:
36+
"""
37+
This method checks whether the input string `s` is a palindrome or not.
38+
It ignores non-alphanumeric characters and is case-insensitive.
39+
40+
Parameters:
41+
s (str): The input string to check.
42+
43+
Returns:
44+
bool: True if the string is a palindrome, False otherwise.
45+
46+
>>> Solution().is_palindrome("Was it a car or a cat I saw?")
47+
True
48+
>>> Solution().is_palindrome("tab a cat")
49+
False
50+
>>> Solution().is_palindrome("")
51+
True
52+
>>> Solution().is_palindrome("A")
53+
True
54+
"""
55+
56+
"""
57+
Initialize two pointers, left (l) at the start, and right (r) at
58+
the end of the string.
59+
"""
60+
left, right = 0, len(s) - 1
61+
62+
while left < right:
63+
# Skip non-alphanumeric characters from the left pointer.
64+
while left < right and not self.is_alpha_numeric(s[left]):
65+
left += 1
66+
# Skip non-alphanumeric characters from the right pointer.
67+
while left < right and not self.is_alpha_numeric(s[right]):
68+
right -= 1
69+
70+
# Compare characters at both pointers in a case-insensitive way.
71+
if s[left].lower() != s[right].lower():
72+
return False
73+
74+
# Move both pointers towards the center.
75+
left, right = left + 1, right - 1
76+
77+
# If all characters match, it is a palindrome.
78+
return True
79+
80+
def is_alpha_numeric(self, c: str) -> bool:
81+
"""
82+
Helper function to check if a character is alphanumeric (A-Z, a-z, 0-9).
83+
84+
Parameters:
85+
c (str): The character to check.
86+
87+
Returns:
88+
bool: True if the character is alphanumeric, False otherwise.
89+
"""
90+
return (
91+
ord("A") <= ord(c) <= ord("Z")
92+
or ord("a") <= ord(c) <= ord("z")
93+
or ord("0") <= ord(c) <= ord("9")
94+
)
95+
96+
97+
if __name__ == "__main__":
98+
import doctest
99+
100+
doctest.testmod()

0 commit comments

Comments
 (0)