Skip to content

Commit 95555e7

Browse files
committed
valid-palindrome
1 parent c5ed177 commit 95555e7

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

valid-palindrome/nowrobin.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function(s) {
6+
// 1. 소문자로 변환 + 알파벳/숫자만 남기기
7+
// (공백, 특수문자 제거)
8+
s = s.toLowerCase().replace(/[^a-z0-9]/g, "");
9+
10+
const length = s.length;
11+
12+
// 2. 문자열의 절반만 순회하면서 양쪽 비교
13+
for (let i = 0; i < length / 2; i++) {
14+
// 왼쪽(i) vs 오른쪽(length - 1 - i)
15+
if (s[i] !== s[length - 1 - i]) {
16+
// 하나라도 다르면 팰린드롬 아님
17+
return false;
18+
}
19+
}
20+
21+
// 3. 끝까지 문제 없으면 팰린드롬
22+
return true;
23+
};

0 commit comments

Comments
 (0)