본문 바로가기
BEB/algorithm

40_longestPalindrome 가장긴 팰린드롬

by ddanss 2022. 12. 20.
728x90
let longestPalindrome = function (str) {
  const isPalindrome = function(s, left, right) { //앞뒤가 같은 팰린드롬인지 확인
    while(left >= 0 && right < s.length) {
      if(s[left] !== s[right]) break;
      left--;
      right++;
    }
    return right-left-1;
  }
  const solution = function (s) {
    let res = 0;
    for(let i=0;i<s.length;i++) {
      let odd = isPalindrome(s, i, i);
      let even = isPalindrome(s, i-1, i);
      let maxx = Math.max(odd, even);
      res = Math.max(res, maxx);
    }
    return res;
  }
  return solution(str);
};
반응형

'BEB > algorithm' 카테고리의 다른 글

45_subsetSum  (0) 2023.01.09
41_countIslands  (0) 2022.12.22
38_decompression  (0) 2022.12.16
37_coinChange  (0) 2022.12.15
34_LCS (Longest Common Subsequence)  (0) 2022.12.12

댓글