We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1682c3f commit 8e746d4Copy full SHA for 8e746d4
valid-palindrome/solbijae.ts
@@ -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