Skip to content

Commit d858e59

Browse files
committed
feat: implement isPalindrome function with detailed comments on approach and complexity
1 parent 92b6029 commit d858e59

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

valid-palindrome/hongseoupyun.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
6+
// Time complexity = O(n), as it checks, replaces, converts to lowercase, and reverses all characters in the string
7+
// Space complexity = O(n), as it creates a new string with a maximum length equal to the original string
8+
function isPalindrome(s: string): boolean {
9+
let filteredString: string = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
10+
let reversed: string = filteredString.split("").reverse().join("")
11+
12+
if (filteredString === reversed) {
13+
return true
14+
} else {
15+
return false
16+
}
17+
};

0 commit comments

Comments
 (0)