Skip to content

Commit f886424

Browse files
committed
Valid-palindrome solution
1 parent c80e2b7 commit f886424

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

valid-palindrome/joon.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import re
2+
3+
4+
class Solution:
5+
# Time: O(n)
6+
# Space: O(1)
7+
def isPalindrome(self, s: str) -> bool:
8+
# 1. Convert string
9+
# Time: O(n)
10+
# Space: O(n) since re.sub() will internally use a new string of length n.
11+
s = re.sub('[^a-z0-9]', '', s.lower())
12+
length = len(s)
13+
# 2. Check if the string reads the same forward and backward, one by one.
14+
# Time: O(n)
15+
# Space: O(1)
16+
for i in range(length):
17+
if (s[i] != s[length - 1 - i]):
18+
return False
19+
return True

0 commit comments

Comments
 (0)