Skip to content

Commit 30802e9

Browse files
authored
[ PS ] : Valid Palindrome
1 parent a8ad6f0 commit 30802e9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-palindrome/uraflower.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 주어진 문자열이 조건을 만족하는 회문인지 여부를 반환하는 함수
3+
* @param {string} s
4+
* @return {boolean}
5+
*/
6+
const isPalindrome = function(s) {
7+
const filtered = Array.from(s.toLowerCase()).reduce((str, char) => {
8+
return isAlphanumeric(char) ? str + char : str;
9+
}, '');
10+
11+
for (let left = 0, right = filtered.length - 1; left < right; left++, right--) {
12+
if (filtered[left] !== filtered[right]) {
13+
return false;
14+
}
15+
}
16+
17+
return true;
18+
};
19+
20+
21+
function isAlphanumeric(char) {
22+
return char !== ' ' && (('a' <= char && char <= 'z') || !Number.isNaN(Number(char)));
23+
}
24+
25+
// 시간복잡도: O(n)
26+
// 공간복잡도: O(n)

0 commit comments

Comments
 (0)