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 ff613e8 commit a247f61Copy full SHA for a247f61
1 file changed
valid-palindrome/togo26.js
@@ -0,0 +1,28 @@
1
+/**
2
+ * @param {string} s
3
+ * @return {boolean}
4
+ */
5
+// TC: O(n) / SC: O(n)
6
+var isPalindrome = function (s) {
7
+ const processed = s.toLowerCase().replaceAll(/[^a-z0-9]+/g, '');
8
+ const reversed = [...processed].reverse().join('');
9
+ return processed === reversed;
10
+};
11
+
12
+// TC: O(n) / SC: O(1)
13
+// With two pointers
14
15
+ const nonAlphanumeric = new RegExp(/[^a-zA-Z0-9]+/);
16
+ let left = 0;
17
+ let right = s.length - 1;
18
19
+ while (left < right) {
20
+ while (left < right && nonAlphanumeric.test(s[left])) left++; // 단일 문자 테스트 O(1) 상수 취급
21
+ while (left < right && nonAlphanumeric.test(s[right])) right--;
22
+ if (s[left].toLowerCase() !== s[right].toLowerCase()) return false;
23
+ left++;
24
+ right--;
25
+ }
26
27
+ return true;
28
0 commit comments