앞뒤를 뒤집어도 똑같은 문자열을 팰린드롬(palindrome)이라고 합니다.
문자열 s가 주어질 때, s의 부분문자열(Substring)중 가장 긴 팰린드롬의 길이를 return 하는 solution 함수를 완성해 주세요.예를들면, 문자열 s가 "abcdcba"이면 7을 return하고 "abacde"이면 3을 return합니다.
문자열 s의 길이 : 2,500 이하의 자연수
문자열 s는 알파벳 소문자로만 구성
class Solution {
public int solution(String s) {
int ret = 1;
for(int i=0 ; i<s.length()-1; i++) {
int len = Math.max(helper(i, i, s), helper(i, i+1, s));
ret = Math.max(ret, len);
}
return ret;
}
public int helper(int s, int e, String str) {
while(s>=0 && e<str.length() && str.charAt(s)==str.charAt(e)) {
s--;
e++;
}
return e-s-1;
}
}
Leetcode에서 똑같은 문제를 풀었던 기억이 있어서 쉽게 풀었다.