Skip to content

Commit 4226c16

Browse files
committed
- Valid Palindrome #220
1 parent 5cb62e7 commit 4226c16

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

valid-palindrome/ayosecu.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
"""
3+
- Time Complexity: O(n), n = len(s)
4+
- Space Complexity: O(1)
5+
"""
6+
def isPalindrome(self, s: str) -> bool:
7+
# to lower and check two pointers
8+
s = s.lower()
9+
l, r = 0, len(s) - 1
10+
11+
while l <= r:
12+
if not s[l].isalnum():
13+
l += 1
14+
continue
15+
if not s[r].isalnum():
16+
r -= 1
17+
continue
18+
19+
if s[l] != s[r]:
20+
return False
21+
l += 1
22+
r -= 1
23+
24+
return True
25+
26+
27+
tc = [
28+
("A man, a plan, a canal: Panama", True),
29+
("race a car", False),
30+
(" ", True)
31+
]
32+
33+
for i, (s, e) in enumerate(tc, 1):
34+
sol = Solution()
35+
r = sol.isPalindrome(s)
36+
print(f"TC {i} is Passed!" if r == e else f"TC {i} is Failed! - Expected: {e}, Result: {r}")

0 commit comments

Comments
 (0)