Skip to content

Commit 1acbb5e

Browse files
committed
feat: implement isPalindrome function with detailed comments on approach and complexity in python
1 parent d858e59 commit 1acbb5e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

valid-palindrome/hongseoupyun.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring non-alphanumeric characters - letters and number).
2+
# the solution should first filter out non-alphanumeric characters and convert the string to lowercase.
3+
# Then, it should check if the cleaned string is equal to its reverse.
4+
5+
# Time complexity = O(n), as it checks, replaces, converts to lowercase, and reverses all characters in the string
6+
# Space complexity = O(n), as it creates a new string with a maximum length equal to the original string
7+
import re
8+
9+
class Solution:
10+
def isPalindrome(self, s: str) -> bool:
11+
filtered_string = re.sub(r'[^a-zA-Z0-9]','',s).lower()
12+
reversed_string = filtered_string[::-1]
13+
return reversed_string == filtered_string

0 commit comments

Comments
 (0)