标签:
public class Solution { public String longestPalindrome(String s) { if (s == null || s.length() == 0) { return null; } String longest = s.substring(0, 1); for (int i = 0; i < s.length(); i++) { String tmp = helper(s, i, i); //get longest palindromic substring with one letter center i like aba if (tmp.length() > longest.length()) { longest = tmp; } //get longest palindromic substring with two letter, i, i+1 like abba tmp = helper(s, i, i+1); if (tmp.length() > longest.length()) { longest = tmp; } } return longest; } //given a center, one letter or two letter //return longest palindrome 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); } }
标签:
原文地址:http://www.cnblogs.com/77rousongpai/p/4496339.html