Skip to content

Commit d0956f1

Browse files
committed
feat: valid palindrome
1 parent 2d72388 commit d0956f1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

valid-palindrome/anniemon.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 시간 복잡도:
3+
* s.length만큼 탐색. 즉, O(n)
4+
* 공간 복잡도:
5+
* 변수와 함수만 저장. 즉, O(1)
6+
*/
7+
8+
/**
9+
* @param {string} s
10+
* @return {boolean}
11+
*/
12+
var isPalindrome = function(s) {
13+
const isAlphaNumeric = (v) => {
14+
return (/^[a-z0-9]$/i).test(v);
15+
};
16+
17+
let l = 0;
18+
let r = s.length - 1;
19+
while(l < r) {
20+
while(!isAlphaNumeric(s[l]) && l < r) {
21+
l++;
22+
}
23+
while(!isAlphaNumeric(s[r]) && l < r) {
24+
r--;
25+
}
26+
27+
if(s[l].toLowerCase() !== s[r].toLowerCase()) {
28+
return false;
29+
}
30+
l++;
31+
r--;
32+
}
33+
return true;
34+
};

0 commit comments

Comments
 (0)