Skip to content

Commit be09081

Browse files
committed
add solution: valid-palindrome
1 parent 75e6d44 commit be09081

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

valid-palindrome/dusunax.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
# Leetcode 125. Valid Palindrome
3+
4+
use regex to filter out non-alphanumeric characters 🔍
5+
6+
## Time and Space Complexity
7+
8+
```
9+
TC: O(n)
10+
SC: O(n)
11+
```
12+
13+
### TC is O(n):
14+
- iterating through the string just once to filter out non-alphanumeric characters.
15+
16+
### SC is O(n):
17+
- creating a new string to store the filtered characters.
18+
'''
19+
20+
class Solution:
21+
def isPalindrome(self, s: str) -> bool:
22+
if s is " ":
23+
return True
24+
25+
reg = "[^a-z0-9]"
26+
converted_s = re.sub(reg, "", s.lower())
27+
28+
return converted_s == converted_s[::-1]

0 commit comments

Comments
 (0)