Skip to content

Commit 2fde946

Browse files
committed
solve: programmers 가장 긴 팰린드롬
1 parent 96b8a28 commit 2fde946

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

  • src/kyu9341/programmers_가장긴팰린드롬
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const getLongestPalindrome = (left, right, arrFromStr) => {
2+
let maxLength = 0;
3+
const isInRange = (left, right) => left >= 0 && right < arrFromStr.length;
4+
5+
while (isInRange(left, right)) {
6+
if (arrFromStr[left] !== arrFromStr[right]) return maxLength;
7+
8+
maxLength = right - left + 1;
9+
left -= 1;
10+
right += 1;
11+
}
12+
13+
return maxLength;
14+
};
15+
16+
const solution = s => {
17+
let maxLength = 0;
18+
const arrFromStr = [...s];
19+
20+
const checkLength = (_, index) => {
21+
const odd = getLongestPalindrome(index, index, arrFromStr);
22+
const even = getLongestPalindrome(index, index + 1, arrFromStr);
23+
maxLength = Math.max(maxLength, Math.max(odd, even));
24+
};
25+
26+
arrFromStr.forEach(checkLength);
27+
28+
return maxLength;
29+
};
30+
31+
(() => {
32+
const inputs = ['abcdcba', 'abacde'];
33+
34+
inputs.forEach(input => console.log(solution(input)));
35+
})();

0 commit comments

Comments
 (0)