5. 最长回文子串
题目描述
给你一个字符串 s
,找到 s
中最长的 回文 子串。
示例 1:
输入:s = "babad" 输出:"bab" 解释:"aba" 同样是符合题意的答案。
示例 2:
输入:s = "cbbd" 输出:"bb"
提示:
1 <= s.length <= 1000
s
仅由数字和英文字母组成
方法一:动态规划
我们定义
接下来,我们定义变量
考虑
由于
时间复杂度
java
class Solution {
public String longestPalindrome(String s) {
int n = s.length();
boolean[][] f = new boolean[n][n];
for (var g : f) {
Arrays.fill(g, true);
}
int k = 0, mx = 1;
for (int i = n - 2; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
f[i][j] = false;
if (s.charAt(i) == s.charAt(j)) {
f[i][j] = f[i + 1][j - 1];
if (f[i][j] && mx < j - i + 1) {
mx = j - i + 1;
k = i;
}
}
}
}
return s.substring(k, k + mx);
}
}
cpp
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
vector<vector<bool>> f(n, vector<bool>(n, true));
int k = 0, mx = 1;
for (int i = n - 2; ~i; --i) {
for (int j = i + 1; j < n; ++j) {
f[i][j] = false;
if (s[i] == s[j]) {
f[i][j] = f[i + 1][j - 1];
if (f[i][j] && mx < j - i + 1) {
mx = j - i + 1;
k = i;
}
}
}
}
return s.substr(k, mx);
}
};
ts
function longestPalindrome(s: string): string {
const n = s.length;
const f: boolean[][] = Array(n)
.fill(0)
.map(() => Array(n).fill(true));
let k = 0;
let mx = 1;
for (let i = n - 2; i >= 0; --i) {
for (let j = i + 1; j < n; ++j) {
f[i][j] = false;
if (s[i] === s[j]) {
f[i][j] = f[i + 1][j - 1];
if (f[i][j] && mx < j - i + 1) {
mx = j - i + 1;
k = i;
}
}
}
}
return s.slice(k, k + mx);
}
python
class Solution:
def longestPalindrome(self, s: str) -> str:
n = len(s)
f = [[True] * n for _ in range(n)]
k, mx = 0, 1
for i in range(n - 2, -1, -1):
for j in range(i + 1, n):
f[i][j] = False
if s[i] == s[j]:
f[i][j] = f[i + 1][j - 1]
if f[i][j] and mx < j - i + 1:
k, mx = i, j - i + 1
return s[k : k + mx]
方法二:枚举回文中间点
我们可以枚举回文中间点,向两边扩散,找到最长的回文串。
时间复杂度
java
class Solution {
private String s;
private int n;
public String longestPalindrome(String s) {
this.s = s;
n = s.length();
int start = 0, mx = 1;
for (int i = 0; i < n; ++i) {
int a = f(i, i);
int b = f(i, i + 1);
int t = Math.max(a, b);
if (mx < t) {
mx = t;
start = i - ((t - 1) >> 1);
}
}
return s.substring(start, start + mx);
}
private int f(int l, int r) {
while (l >= 0 && r < n && s.charAt(l) == s.charAt(r)) {
--l;
++r;
}
return r - l - 1;
}
}
cpp
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
int start = 0, mx = 1;
auto f = [&](int l, int r) {
while (l >= 0 && r < n && s[l] == s[r]) {
l--, r++;
}
return r - l - 1;
};
for (int i = 0; i < n; ++i) {
int a = f(i, i);
int b = f(i, i + 1);
int t = max(a, b);
if (mx < t) {
mx = t;
start = i - (t - 1 >> 1);
}
}
return s.substr(start, mx);
}
};
python
class Solution:
def longestPalindrome(self, s: str) -> str:
def f(l, r):
while l >= 0 and r < n and s[l] == s[r]:
l, r = l - 1, r + 1
return r - l - 1
n = len(s)
start, mx = 0, 1
for i in range(n):
a = f(i, i)
b = f(i, i + 1)
t = max(a, b)
if mx < t:
mx = t
start = i - ((t - 1) >> 1)
return s[start : start + mx]