leetcode -- 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.
O(n*n)。对于每一个字符,以之作为中间元素往左右寻找。注意处理奇偶两种模式:
1. aba
2. abba
1 public class Solution { 2 public String longestPalindrome(String s) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 String longestPalindrome = ""; 6 for(int i = 0; i < s.length(); i++){ 7 8 // 需考虑两种情形 9 // aba 和 abba 10 String oddPattern = getOddPalindromeSubstring(i, s); 11 String evenPattern = getEvenPalindromeSubstring(i, s); 12 if(evenPattern.length() > oddPattern.length()){ 13 if(evenPattern.length() > longestPalindrome.length()) 14 longestPalindrome = evenPattern; 15 } else { 16 if(oddPattern.length() > longestPalindrome.length()) 17 longestPalindrome = oddPattern; 18 } 19 } 20 21 return longestPalindrome; 22 } 23 24 // abba 25 public String getEvenPalindromeSubstring(int index, String s){ 26 char currentChar = s.charAt(index); 27 int leftPointer = index - 1; 28 int rightPointer = index + 1; 29 //String result = ""; 30 // 回文子串至少包含当前字符 31 String result = ""; 32 result += currentChar; 33 34 /* 35 if(leftPointer >= 0 && s.charAt(leftPointer) == currentChar){ 36 result = s.substring(leftPointer, index + 1); 37 leftPointer --; 38 } 39 */ 40 41 if(rightPointer < s.length() && s.charAt(rightPointer) == currentChar){ 42 result += s.charAt(rightPointer); 43 rightPointer ++; 44 } 45 46 while(leftPointer >= 0 && rightPointer < s.length()){ 47 if(s.charAt(leftPointer) == s.charAt(rightPointer)){ 48 result = s.substring(leftPointer, rightPointer + 1); 49 leftPointer --; 50 rightPointer ++; 51 continue; 52 } 53 break; 54 } 55 return result; 56 57 } 58 59 // aba 60 public String getOddPalindromeSubstring(int index, String s){ 61 char currentChar = s.charAt(index); 62 int leftPointer = index - 1; 63 int rightPointer = index + 1; 64 //String result = ""; 65 String result = ""; 66 // 回文子串至少包含当前字符 67 result += currentChar; 68 69 while(leftPointer >= 0 && rightPointer < s.length()){ 70 if(s.charAt(leftPointer) == s.charAt(rightPointer)){ 71 result = s.substring(leftPointer, rightPointer + 1); 72 leftPointer --; 73 rightPointer ++; 74 continue; 75 } 76 break; 77 } 78 return result; 79 } 80 }
find a more elegant code:http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html
1 string expandAroundCenter(string s, int c1, int c2) { 2 int l = c1, r = c2; 3 int n = s.length(); 4 while (l >= 0 && r <= n-1 && s[l] == s[r]) { 5 l--; 6 r++; 7 } 8 return s.substr(l+1, r-l-1); 9 } 10 11 string longestPalindromeSimple(string s) { 12 int n = s.length(); 13 if (n == 0) return ""; 14 string longest = s.substr(0, 1); // a single char itself is a palindrome 15 for (int i = 0; i < n-1; i++) { 16 string p1 = expandAroundCenter(s, i, i); 17 if (p1.length() > longest.length()) 18 longest = p1; 19 20 string p2 = expandAroundCenter(s, i, i+1); 21 if (p2.length() > longest.length()) 22 longest = p2; 23 } 24 return longest; 25 }
时间复杂度为O(n)的算法
http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html