5. Longest Palindromic Substring

题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

public class Solution {
    public String longestPalindrome(String s) {
        if(s.isEmpty())
           return null;
        if(s.length()==1)
           return s;
        int n=s.length();
    //  String rec=s.substring(0,1);
        String max=s.substring(0,1);
        for(int i=0;i=rec2.length())
                   max=rec1;
                else
                   max=rec2;
            }
        return max;
    }
   
    public String helper(String s,int begin,int end)
    {
    while(begin>=0&&end<=s.length()-1&&s.charAt(begin)==s.charAt(end))
        {
            begin--;
            end++;
        }
    return s.substring(begin+1,end);
    }
}

你可能感兴趣的:(5. Longest Palindromic Substring)