Skip to content

Commit f1d5d77

Browse files
committed
solve: valid palindrome
1 parent 1f55061 commit f1d5d77

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

valid-palindrome/wogha95.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,70 @@
1+
// 3차
2+
// TC: O(N)
3+
// SC: O(N)
4+
// N: s.length
5+
6+
/**
7+
* @param {string} s
8+
* @return {boolean}
9+
*/
10+
var isPalindrome = function (s) {
11+
// 1. 투포인터를 양끝에서 시작합니다.
12+
let left = 0;
13+
let right = s.length - 1;
14+
15+
while (left < right) {
16+
// 2. 현재 가리키는 문자가 영대소문자, 숫자가 아니면 건너뜁니다.
17+
while (left < right && !isValid(s[left])) {
18+
left += 1;
19+
}
20+
while (left < right && !isValid(s[right])) {
21+
right -= 1;
22+
}
23+
24+
// 3. 문자의 갯수가 홀수인 경우 투 포인터가 모두 가운데를 가리키는 경우 순회를 끝났다고 생각합니다.
25+
if (left === right) {
26+
break;
27+
}
28+
29+
// 4. 투 포인터가 가리키는 문자가 다른 경우 정답(false)를 반환합니다.
30+
if (!isSame(s[left], s[right])) {
31+
return false;
32+
}
33+
34+
// 5. 다음 문자로 넘어갑니다.
35+
left += 1;
36+
right -= 1;
37+
}
38+
39+
// 6. 모든 순회가 끝냈다면 palindrome이라고 판단합니다.
40+
return true;
41+
42+
function isValid(spell) {
43+
if ("0" <= spell && spell <= "9") {
44+
return true;
45+
}
46+
if ("a" <= spell && spell <= "z") {
47+
return true;
48+
}
49+
if ("A" <= spell && spell <= "Z") {
50+
return true;
51+
}
52+
53+
return false;
54+
}
55+
56+
function isSame(spellA, spellB) {
57+
if (spellA === spellB) {
58+
return true;
59+
}
60+
if (Math.abs(s[left].charCodeAt() - s[right].charCodeAt()) === 32) {
61+
return true;
62+
}
63+
64+
return false;
65+
}
66+
};
67+
168
// 2차
269
// TC: O(N)
370
// SC: O(N)

0 commit comments

Comments
 (0)