最长回文子串

Finding the longest palindromic substring is a classic problem of coding interview. This post summarizes 3 different solutions for this problem.

要求:

给定一个字符串S,找出它的最长回文子串。假定S的最大长度为1000,且最长回文子串唯一

 

难度:中等

 

分析:

假定字符串s为回文字符串,则在s头部和尾部分别添加相同字符串[x],所得结果s'=[x]s[x]也为回文字符串(论述1)。可使用动态规划方法解决此问题,递推公式便基于此特性。

创建一个布尔二维数组plain,plain[i][j]为true表示原字符串从索引i到索引j这一段子串为回文。而plain[i][j]为true的前提是s.charAt(i)与s.charAt(j)相同,且以下两个条件满足其一:

1. j-i<=2, j=i时必然成立,j-i=1则表示i与j相邻,"aa"、"bb"满足条件

2. j-i>2, 此时须plain[i+1][j-1]为true,即论述1所述

核心算法分析完毕,写一双重循环,索引i由字符串尾部向前遍历,索引j由i向后遍历,每次发现回文字符串时设置二维数组相应的值,并比较其长度是否大于当前的maxLen。若大于,则更新用于记录当前最长回文子串起始和终止位置的start和end

 

解决方案:

Java - 396ms

Java代码   收藏代码
  1. public String longestPalindrome(String s) {  
  2.         if(s.length()<=1){  
  3.             return s;  
  4.         }  
  5.         int start=0, end=0;  
  6.         int maxLen = 0;  
  7.         boolean[][] plain = new boolean[s.length()][s.length()];  
  8.         for(int i=s.length()-1;i>=0;i--){  
  9.             for(int j=i;j<s.length();j++){  
  10.                 if(s.charAt(i)==s.charAt(j)&&(j-i<=2||plain[i+1][j-1])){  
  11.                     plain[i][j]=true;  
  12.                     if(maxLen<j-i+1){  
  13.                         maxLen = j-i+1;  
  14.                         start = i;  
  15.                         end = j;  
  16.                     }  
  17.                 }  
  18.             }  
  19.         }  
  20.         return s.substring(start, end+1);  
  21.     }  

 简单测试程序


1. Dynamic Programming

Let s be the input string, i and j are two indices of the string. Define a 2-dimension array "table" and let table[i][j] denote whether a substring from i to j is palindrome.

Changing condition:

table[i+1][j-1] == 1 && s.charAt(i) == s.charAt(j)
=>
table[i][j] == 1

Time O(n^2) Space O(n^2)

	
  
  
  1. public String longestPalindrome(String s) {  
  2.         if(s = null ||s.length()<=1){  
  3.             return s;  
  4.         }  
  5.         int len = s.length;  
  6.         int maxLen = 1;  
  7.         boolean[][] dp = new boolean[len][len]; 
  8. String longest = null; 
  9.         for(int l=0;l<s.length()-1;l++){  
  10.             for(int i=0;i<len-1;i++){
  11. j=i+1;  
  12.                 if(s.charAt(i)==s.charAt(j)&&(j-i<=2||dp[i+1][j-1])){  
  13.                     dp[i][j]=true 
  14.                     if(maxLen<j-i+1){  
  15.                         maxLen = j-i+1;  
  16.                         longest = s.substring(i, j+1);
  17.                     }  
  18.                 }  
  19.             }  
  20.         }  
  21.         return longest;  
  22.     }  

For example, if the input string is "dabcba", the final matrix would be the following:

1 0 0 0 0 0 
0 1 0 0 0 1 
0 0 1 0 1 0 
0 0 0 1 0 0 
0 0 0 0 1 0 
0 0 0 0 0 1 

From the table, we can clearly see that the longest string is in cell table[1][5].


第二种方法“是对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串abc,中心可以是a,b,c,或者是ab的间隙,bc的间隙,例如aba是回文,abba也是回文,这两种情况要分情况考虑)往两边同时进 行扫描,直到不是回文串为止。假设字符串的长度为n,那么中心的个数为2*n-1(字符作为中心有n个,间隙有n-1个)。对于每个中心往两边扫描的复杂 度为O(n),所以时间复杂度为O((2*n-1)*n)=O(n^2),空间复杂度为O(1)。”引自Code ganker(http://codeganker.blogspot.com/2014/02/longest-palindromic-substring-leetcode.html)

代码如下:

 

复制代码
 1      public String longestPalindrome(String s) {
 2          if (s.isEmpty()||s== null||s.length() == 1)
 3              return s;
 4      
 5         String longest = s.substring(0, 1);
 6          for ( int i = 0; i < s.length(); i++) {
 7              //  get longest palindrome with center of i
 8              String tmp = helper(s, i, i);
 9             
10              if (tmp.length() > longest.length())
11                 longest = tmp;
12      
13              //  get longest palindrome with center of i, i+1
14              tmp = helper(s, i, i + 1);
15              if (tmp.length() > longest.length())
16                 longest = tmp;
17         }
18      
19          return longest;
20     }
21      
22      //  Given a center, either one letter or two letter, 
23       //  Find longest palindrome
24       public String helper(String s,  int begin,  int end) {
25          while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) {
26             begin--;
27             end++;
28         }
29          return s.substring(begin + 1, end);
30     }
复制代码

 Reference:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值