Skip to content

Commit 8e746d4

Browse files
authored
valid-palindrome solution
1 parent 1682c3f commit 8e746d4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-palindrome/solbijae.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function isPalindrome(s: string): boolean {
2+
// 첫 시도: 시간/공간 복잡도 O(n)
3+
// const filtered = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
4+
// if (filtered === filtered.split('').reverse().join('')) return true;
5+
// return false;
6+
7+
// 두번째 시도: Two Pointer 사용 - 시간 복잡도 O(n), 공간 복잡도 O(1)
8+
let left = 0;
9+
let right = s.length - 1;
10+
11+
while (left < right) {
12+
while (left < right && !isAlphanumeric(s[left])) left++;
13+
while (left < right && !isAlphanumeric(s[right])) right--;
14+
15+
if (s[left].toLowerCase() !== s[right].toLowerCase()) return false;
16+
17+
left++;
18+
right--;
19+
}
20+
21+
return true;
22+
23+
function isAlphanumeric(c: string): boolean {
24+
return /^[a-zA-Z0-9]$/.test(c);
25+
}
26+
};

0 commit comments

Comments
 (0)