Skip to content

Commit fec6b72

Browse files
committed
add solution: valid-palindrome
1 parent e603d16 commit fec6b72

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
2+
3+
/**
4+
* @param {string} s
5+
* @return {boolean}
6+
*/
7+
var isPalindrome = function (s) {
8+
// ์ „์ฒ˜๋ฆฌ - ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž๋งŒ ๋‚จ๊ธฐ๊ณ  ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜
9+
const cleanString = s.toLowerCase().replace(/[^a-z0-9]/g, "");
10+
11+
// ์–‘ ๋์—์„œ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™ํ•˜๋ฉฐ ํ™•์ธ
12+
let left = 0, right = cleanString.length - 1;
13+
14+
while (left < right) {
15+
if (cleanString[left] !== cleanString[right]) {
16+
return false; // ๋Œ€์นญ์ด ๊นจ์ง€๋ฉด false
17+
}
18+
left++;
19+
right--;
20+
}
21+
22+
return true; // ๋Œ€์นญ์ด ์œ ์ง€๋˜๋ฉด true
23+
};

0 commit comments

Comments
ย (0)