问题描述

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.

所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的。比如”a” , “aaabbaaa”

之前去笔试了三星研究院,写算法题的时候限定了编程语言只能使用的头文件和库函数,这在很大程度上考察了一个程序员的单位时间生产力。比如java只能用util包,c/c++语言只能包含以下三个头文件:

stdio.h

malloc.h //ANSI标准建议使用stdlib.h头文件

iostream.h // 非标准输入输出,不需要命名空间

所以我想,针对这种高标准的要求,以后做leetcode系列时应该写三个版本,c语言版本不使用库函数,c++版本使用STL,python版本

解决方案

1.暴力方案(Brute Force)

对于字符串的每一个子串,都判断一下是不是回文字符串,完后返回最长的那一个

(Brute Force) [Time Limit Exceeded]

时间复杂度分析:O(n3),空间复杂度O(n),显然超时了。

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
char result[1000]={0}; bool isHuiwen(int begin,int end,char* s)
{
if (end==begin||end<begin)
{
return true;
}
if (s[begin]!=s[end])
{
return false;
}
return isHuiwen(begin+1,end-1,s);
} char* longestHuiwen(int length,char* s)
{
int begin = 0,end=0,sum=0;
for (int i=0;i<length;i++)
{
for (int j=0;j<=i;j++)
{
if (isHuiwen(j,i,s))
{
if (i-j>=sum)
{
sum = i -j;
begin = j;
end = i;
} } }
}
strncpy(result,s+begin,sum+1);//由0开始计数
return result;
} int _tmain(int argc, _TCHAR* argv[])
{
char* s = "abcabaaaabbacabbaa";
char* r_s = longestHuiwen(18,s);
return 0;
}

2.问题转换为求最长相似子串

Approach #1 (Longest Common Substring) [Accepted]

Common mistake

Some people will be tempted to come up with a quick solution, which is unfortunately flawed (however can be corrected easily):

Reverse S and become S′.

Find the longest common substring between S and S​′, which must also be the longest palindromic substring.This seemed to work, let’s see some examples below.

For example,

S=”caba”

S′=”abac”

The longest common substring between S and S​′ is ”aba”, which is the answer.

Let’s try another example:

S=”abacdfgdcaba”

S′=”abacdgfdcaba”

The longest common substring between S and S​′ is ”abacd”

Clearly, this is not a valid palindrome.

讨论帖子: http://bbs.csdn.net/topics/392005408

其他三种解法

Approach #3 (Dynamic Programming) [Accepted]

To improve over the brute force solution, we first observe how we can avoid unnecessary re-computation while validating palindromes. Consider the case

”ababa”

”ababa”. If we already knew that

”bab”

”bab” is a palindrome, it is obvious that

”ababa”

”ababa” must be a palindrome since the two left and right end letters are the same.

We define P(i,j)P(i,j) as following:

P(i,j)={true,

if the substring Si…Sj is a palindrome

false,

otherwise.

P(i,j)={true,if the substring Si…Sj is a palindromefalse,otherwise.

Therefore,

P(i, j) = ( P(i+1, j-1) \text{ and } S_i == S_j ) P(i,j)=(P(i+1,j−1) and S​i==S​j)

The base cases are:

P(i, i) = true P(i,i)=true

P(i, i+1) = ( S_i == S_{i+1} ) P(i,i+1)=(S​i ==Si+1)

This yields a straight forward DP solution, which we first initialize the one and two letters palindromes, and work our way up finding all three letters palindromes, and so on…

Complexity Analysis

Time complexity : O(n^2)O(n​2). This gives us a runtime complexity of O(n^2)O(n2).

Space complexity : O(n^2)O(n​2). It uses O(n^2)O(n2) space to store the table.

Additional Exercise

Could you improve the above space complexity further and how?

Approach #4 (Expand Around Center) [Accepted]

In fact, we could solve it in O(n^2)O(n​2 ) time using only constant space.

We observe that a palindrome mirrors around its center. Therefore, a palindrome can be expanded from its center, and there are only 2n - 12n−1 such centers.

You might be asking why there are 2n - 12n−1 but not nn centers? The reason is the center of a palindrome can be in between two letters. Such palindromes have even number of letters (such as

”abba””abba”) and its center are between the two ‘b”b’s.

public String longestPalindrome(String s) {

int start = 0, end = 0;

for (int i = 0; i < s.length(); i++) {

int len1 = expandAroundCenter(s, i, i);

int len2 = expandAroundCenter(s, i, i + 1);

int len = Math.max(len1, len2);

if (len > end - start) {

start = i - (len - 1) / 2;

end = i + len / 2;

}

}

return s.substring(start, end + 1);

}

private int expandAroundCenter(String s, int left, int right) {

int L = left, R = right;

while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {

L–;

R++;

}

return R - L - 1;

}

Complexity Analysis

Time complexity : O(n^2)O(n​2​​ ). Since expanding a palindrome around its center could take O(n)O(n) time, the overall complexity is O(n^2)O(n​2​​ ).

Space complexity : O(1)O(1).

Approach #5 (Manacher’s Algorithm) [Accepted]

There is even an O(n)O(n) algorithm called Manacher’s algorithm, explained here in detail. However, it is a non-trivial algorithm, and no one expects you to come up with this algorithm in a 45 minutes coding session. But, please go ahead and understand it, I promise it will be a lot of fun.

参考代码

c代码

char* longestPalindrome(char* s) {
int i,length=strlen(s);
char* new_s;
new_s=malloc(sizeof(char)*(2*length + 2));
new_s[0]='$';
new_s[1]='#'; for(i=0;i<length;i++)
{
*(new_s+2*i+2)=s[i];
*(new_s+2*i+3)='#'; }
int len=2*length + 2;
int* r;
r=malloc(sizeof(int)*len);
r[0]=0;
int center=1;
int max_right=0;
for(i=1;i<len;i++)
{
if(i<max_right)
{
if( (max_right-i)> r[2*center-i] )
r[i]=r[2*center-i];
else
r[i]=(max_right-i);
}
else r[i]=1;
while(new_s[i-r[i]]==new_s[i+r[i]] && i-r[i]>0 && i+r[i]<len)
{
r[i]++;
} if(i+r[i] > max_right)
{
center = i;
max_right = i+r[i]; } }
int max_r = 0;
int j=0;
for(i=1;i<len;i++)
{
if( max_r<r[i])
{
j=i;
max_r= r[i];
}
}
int m=(j-(max_r-2)-2)/2;
int n=(j+(max_r-2)-2)/2;
char *c;
c=malloc((max_r)*sizeof(char)); int x=0;
for(i=m;i<=n,x<max_r-1;i++)
{
c[x]=s[i];
x++;
}
*(c+max_r-1)='\0';
return c;
free(r);
free(new_s);
free(c); }

c++代码

string longestPalindrome(string s) {
if (s.empty()) return"";
if (s.size() == 1) return s;
int min_start = 0, max_len = 1;
for (int i = 0; i < s.size();) {
if (s.size() - i <= max_len / 2) break;
int j = i, k = i;
while (k < s.size()-1 && s[k+1] == s[k]) ++k; // Skip duplicate characters.
i = k+1;
while (k < s.size()-1 && j > 0 && s[k + 1] == s[j - 1]) { ++k; --j; } // Expand.int new_len = k - j + 1;
if (new_len > max_len) { min_start = j; max_len = new_len; }
}
return s.substr(min_start, max_len);
}

python参考代码


def longestPalindrome(self, s):
res = ""
for i in xrange(len(s)):
# odd case, like "aba"
tmp = self.helper(s, i, i)
if len(tmp) > len(res):
res = tmp
# even case, like "abba"
tmp = self.helper(s, i, i+1)
if len(tmp) > len(res):
res = tmp
return res # get the longest palindrome, l, r are the middle indexes
# from inner to outer
def helper(self, s, l, r):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1; r += 1
return s[l+1:r]

参考文献

http://articles.leetcode.com/longest-palindromic-substring-part-ii/

https://www.felix021.com/blog/read.php?2040

https://leetcode.com/articles/longest-palindromic-substring/

leetcode 5 Longest Palindromic Substring--最长回文字符串的更多相关文章

  1. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  2. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  3. [leetcode]5. Longest Palindromic Substring最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  4. 转载-----Java Longest Palindromic Substring(最长回文字符串)

    转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...

  5. Longest Palindromic Substring (最长回文字符串)——两种方法还没看,仍需认真看看

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. Java Longest Palindromic Substring(最长回文字符串)

    假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic string.如aba,或者abba.本题是这种,给定输入一个字符串.要求输出一个子串,使得子串是最长的padro ...

  7. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  8. 1. Longest Palindromic Substring ( 最长回文子串 )

    要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...

  9. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  10. 【LeetCode】5. Longest Palindromic Substring 最长回文子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

随机推荐

  1. Batch训练的反向传播过程

    Batch训练的反向传播过程 本文试图通过Softmax理解Batch训练的反向传播过程 采用的网络包含一层全连接和一层softmax,具体网络如下图所示: 交叉熵成本函数: \[L = - \fra ...

  2. [ABP]浅谈模块系统与 ABP 框架初始化

    在 ABP 框架当中所有库以及项目都是以模块的形式存在,所有模块都是继承自AbpModule 这个抽象基类,每个模块都拥有四个生命周期.分别是: PreInitialze(); Initialize( ...

  3. 【转】C++ STL快速入门

    转自:https://www.cnblogs.com/skyfsm/p/6934246.html 冠军的试炼 悟已往之不谏,知来者之可追 博客园 首页 新随笔 联系 订阅 管理 随笔 - 60  文章 ...

  4. [LeetCode] Teemo Attacking 提莫攻击

    In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  5. python selenum 爬取淘宝

    # -*- coding:utf-8 -*- # author : yesehngbao # time:2018/3/29 import re import pymongo from lxml imp ...

  6. 使用Nwjs开发桌面应用体验

    之前一直用.net开发桌面应用,最近由于公司需要转为nodejs,但也是一直用nodejs开发后台应用,网站,接口等.近期,需要开发一个客户端,想着既然nodejs号称全栈,就试一下开发桌面应用到底行 ...

  7. bzoj 3924: [Zjoi2015]幻想乡战略游戏

    Description 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网游厂商把游戏的地图越做越大,以至于幽香一眼根本看不过来, ...

  8. ●BZOJ 1854 [Scoi2010]游戏

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1854 题解: 并查集(还可以用匈牙利算法进行单路增广的二分图匹配) 把每个武器看成是一条边, ...

  9. 【Codeforces Round 431 (Div. 2) A B C D E五个题】

    先给出比赛地址啦,感觉这场比赛思维考察非常灵活而美妙. A. Odds and Ends ·述大意:      输入n(n<=100)表示长度为n的序列,接下来输入这个序列.询问是否可以将序列划 ...

  10. 树莓派超声波测距+蜂鸣器(c语言)

    前边我们已经详细的讲解了树莓派控制超声波模块测距(http://www.cnblogs.com/yuemo/p/8888342.html)和超声波控制蜂鸣器模块发声(http://www.cnblog ...