Skip to content

Commit 5f005ff

Browse files
committed
solve: valid palindrome
1 parent 208ccc7 commit 5f005ff

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

β€Žvalid-palindrome/tolluset.tsβ€Ž

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function isPalindrome(s: string): boolean {
6+
const parsedString = s.replace(/[^A-Za-z0-9]/g, "").toLowerCase();
7+
const n = parsedString.length;
8+
9+
if (n === 0) {
10+
return true;
11+
}
12+
13+
for (let i = 0; i < n / 2; i++) {
14+
if (parsedString[i] !== parsedString[n - i - 1]) {
15+
return false;
16+
}
17+
}
18+
19+
return true;
20+
}
21+
22+
// Time complexity: O(n)
23+
24+
const t1 = isPalindrome("A man, a plan, a canal: Panama");
25+
console.info("πŸš€ : tolluset.ts:18: t1=", t1);
26+
27+
const t2 = isPalindrome("race a car");
28+
console.info("πŸš€ : tolluset.ts:21: t2=", t2);
29+
30+
const t3 = isPalindrome(" ");
31+
console.info("πŸš€ : tolluset.ts:24: t3=", t3);

0 commit comments

Comments
Β (0)