Skip to content

Commit 0dab8a7

Browse files
Jeehay28Jeehay28
authored andcommitted
Add valid-palindrome solution in TypeScript
1 parent 872feec commit 0dab8a7

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

valid-palindrome/Jeehay28.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Approach 2
2+
// 🗓️ 2025-04-14
3+
// ⏳ Time Complexity: O(n)
4+
// 💾 Space Complexity: O(1)
5+
6+
function isPalindrome(s: string): boolean {
7+
let low = 0, high = s.length - 1;
8+
const reg = /[0-9a-zA-Z]/;
9+
10+
while (low < high) {
11+
while (low < high && !reg.test(s[low])) {
12+
low += 1;
13+
}
14+
15+
while (low < high && !reg.test(s[high])) {
16+
high -= 1;
17+
}
18+
19+
if (s[low].toLowerCase() !== s[high].toLowerCase()) {
20+
return false;
21+
}
22+
low += 1;
23+
high -= 1;
24+
}
25+
26+
return true;
27+
}
28+
29+
30+
// Approach 1
31+
// 🗓️ 2025-04-14
32+
// ⏳ Time Complexity: O(n)
33+
// 💾 Space Complexity: O(n)
34+
35+
// function isPalindrome(s: string): boolean {
36+
// // cover all uppercase letters into lowercasse letters
37+
// // remove all non-alphanumeric characters (letters and numbers)
38+
// // it reads the same forward and backward
39+
40+
// const cleaned = s.toLowerCase().match(/[0-9a-z]/g);
41+
42+
// // Converting to lowercase (toLowerCase()): O(n), where n is the length of the string
43+
// // Matching with regex (match(/[0-9a-z]/g)): O(n), where n is the length of the string, as the regex engine needs to check each character in the string
44+
// // join("") operation: O(m), where m is the length of the cleaned array.
45+
// // reverse() operation: O(m), where m is the length of the cleaned array.
46+
// // Comparison (s_forward === s_backward): O(n) because it checks each character one by one
47+
48+
// if (cleaned !== null) {
49+
// const s_forward = cleaned.join("");
50+
// const s_backward = cleaned.reverse().join("");
51+
// return s_forward === s_backward;
52+
// } else {
53+
// return true;
54+
// }
55+
// }

0 commit comments

Comments
 (0)