[LeetCode] Longest Palindromic Substring

简介: This problem has a long story. There are just too many solutions on the web and it can be studied for a long time before you fully grasp it.

This problem has a long story. There are just too many solutions on the web and it can be studied for a long time before you fully grasp it. Morever, it can induce many other concepts or problems (longest palindromic subsequence, longest common substring, etc).

The simplest way to solve it is to use two-dimensional DP. We denote P[i][j] to be an indicator of whether the substring from i to j (inclusive) is a palindrome. It is obvious that the following relationships hold:

  1. P[i][i] = 1 (each character itself is palindromic);
  2. P[i][i + 1] = s[i] == s[j] (two neighboring characters are palindromic if they are the same);
  3. P[i][j] = P[i + 1][j - 1] && s[i] == s[j] (If the substring is palindrome, then adding the same character at both of its two ends still gives a palindrome).

1 and 2 are base cases and 3 is the general case.

Then we will have the following unoptimiezd DP code.

 1     string longestPalindrome(string s) {
 2         int start = 0, len = 1, n = s.length();
 3         bool dp[1000][1000] = {false};
 4         for (int i = 0; i < n; i++)
 5             dp[i][i] = true;
 6         for (int i = 0; i < n - 1; i++) {
 7             dp[i][i + 1] = s[i] == s[i + 1];
 8             if (dp[i][i + 1]) {
 9                 start = i;
10                 len = 2;
11             }
12         }
13         for (int l = 3; l <= n; l++) {
14             for (int i = 0; i < n - l + 1; i++) {
15                 int j = i + l - 1;
16                 dp[i][j] = dp[i + 1][j - 1] && s[i] == s[j];
17                 if (dp[i][j]) {
18                     start = i;
19                     len = l;
20                 }
21             }
22         }
23         return s.substr(start, len);
24     }

Note that each time when we update dp[i][j], we only need dp[i + 1][j - 1] from the left column, so we can maintain a single variable for it and reduce the space complexity from O(n^2) to O(n). The code now becomes as follows.

 1     string longestPalindrome(string s) {
 2         int start = 0, len = 1, n = s.length();
 3         bool cur[1000] = {false};
 4         bool pre;
 5         cur[0] = true;
 6         for (int j = 1; j < n; j++) {
 7             cur[j] = true;
 8             pre = cur[j - 1];
 9             cur[j - 1] = s[j - 1] == s[j];
10             if (cur[j - 1] && len < 2) {
11                 start = j - 1;
12                 len = 2;
13             }
14             for (int i = j - 2; i >= 0; i--) {
15                 bool temp = cur[i];
16                 cur[i] = pre && s[i] == s[j];
17                 if (cur[i] && j - i + 1 > len) {
18                     start = i;
19                     len = j - i + 1;
20                 }
21                 pre = temp;
22             }
23         }
24         return s.substr(start, len);
25     }

We may also traverse the string and expand to left and right from any character to obtain the longest palindrome. The following code should be self-explanatory.

 1     string search(string s, int left, int right) {
 2         int l = left, r = right;
 3         while (l >= 0 && r < s.length() && s[l] == s[r]) {
 4             l--;
 5             r++;
 6         }
 7         return s.substr(l + 1, r - l - 1);
 8     }
 9     
10     string longestPalindrome(string s) {
11         string longest = s.substr(0, 1);
12         for (int i = 0; i < s.length() - 1; i++) {
13             string tmp1 = search(s, i, i);
14             string tmp2 = search(s, i, i + 1);
15             if (tmp1.length() > longest.length()) longest = tmp1;
16             if (tmp2.length() > longest.length()) longest = tmp2;
17         }
18         return longest;
19     }

Of course, this problem still has a non-trivial O(n) algorithm, named Manacher's algorithm. This page has a nice explanation for it. The final code is shown below.

 1     string process(string s) {
 2         int n = s.length();
 3         string t(2 * n + 3, '#');
 4         t[0] = '$';
 5         t[2 * n + 2] = '%';
 6         for (int i = 0; i < n; i++)
 7             t[2 * (i + 1)] = s[i];
 8         return t;
 9     }
10     
11     string longestPalindrome(string s) {
12         string t = process(s);
13         int n = t.length();
14         int* plen = new int[n]();
15         int center = 0, right = 0;
16         for (int i = 1; i < n - 1; i++) {
17             int i_mirror = 2 * center - i;
18             plen[i] = right > i ? min(plen[i_mirror], right - i) : 0;
19             while (t[i + plen[i] + 1] == t[i - plen[i] - 1])
20                 plen[i]++;
21             if (i + plen[i] > right) {
22                 center = i;
23                 right = i + plen[i];
24             }
25         }
26         int maxlen = 0;
27         for (int i = 1; i < n - 1; i++) {
28             if (plen[i] > maxlen) {
29                 center = i;
30                 maxlen = plen[i];
31             }
32         }
33         delete[] plen;
34         return s.substr((center - 1 - maxlen) / 2, maxlen);
35     }
目录
相关文章
|
5月前
Leetcode 516. Longest Palindromic Subsequence
找到一个字符串的最长回文子序列,这里注意回文子串和回文序列的区别。子序列不要求连续,子串(substring)是要求连续的。leetcode 5. Longest Palindromic Substring就是求连续子串的。
22 0
|
5月前
|
Java
Leetcode 3. Longest Substring Without Repeating Characters
此题题意是找出一个不包含相同字母的最长子串,题目给出了两个例子来说明题意,但这两个例子具有误导性,让你误以为字符串中只有小写字母。还好我是心机boy,我把大写字母的情况也给考虑进去了,不过。。。。字符串里竟然有特殊字符,于是贡献了一次wrong answer,这次我把ascii字符表都考虑进去,然后就没问题了。这个故事告诫我们在编程处理问题的时候一定要注意输入数据的范围,面试中可以和面试官去确认数据范围,也能显得你比较严谨。
27 3
LeetCode 424. Longest Repeating Character Replacem
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次。在执行上述操作后,找到包含重复字母的最长子串的长度。
75 0
LeetCode 424. Longest Repeating Character Replacem
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
54 0
LeetCode 409. Longest Palindrome
LeetCode 395. Longest Substring with At Least K
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k 。输出 T 的长度。
57 0
LeetCode 395. Longest Substring with At Least K
|
Windows
LeetCode 388. Longest Absolute File Path
我们致力于寻找我们文件系统中文件的最长 (按字符的数量统计) 绝对路径。例如,在上述的第二个例子中,最长路径为 "dir/subdir2/subsubdir2/file2.ext",其长度为 32 (不包含双引号)。
52 0
LeetCode 388. Longest Absolute File Path
|
存储
LeetCode 329. Longest Increasing Path in a Matrix
给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。
45 0
LeetCode 329. Longest Increasing Path in a Matrix
|
算法
LeetCode 300. Longest Increasing Subsequence
给定一个无序的整数数组,找到其中最长上升子序列的长度。
34 0
LeetCode 300. Longest Increasing Subsequence
|
算法
LeetCode 128. Longest Consecutive Sequence
给定一个未排序的整数数组,找出最长连续序列的长度。 要求算法的时间复杂度为 O(n)。
64 0
LeetCode 128. Longest Consecutive Sequence
|
Java 程序员 开发者
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化
本文是《LeetCode第三题(Longest Substring Without Repeating Characters)三部曲》的第二篇,前一篇文章已经列出了完整的解题思路,今天来将此思路转化为具体的Java代码
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化