[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     }
目录
相关文章
Leetcode 516. Longest Palindromic Subsequence
找到一个字符串的最长回文子序列,这里注意回文子串和回文序列的区别。子序列不要求连续,子串(substring)是要求连续的。leetcode 5. Longest Palindromic Substring就是求连续子串的。
63 0
|
Java
Leetcode 3. Longest Substring Without Repeating Characters
此题题意是找出一个不包含相同字母的最长子串,题目给出了两个例子来说明题意,但这两个例子具有误导性,让你误以为字符串中只有小写字母。还好我是心机boy,我把大写字母的情况也给考虑进去了,不过。。。。字符串里竟然有特殊字符,于是贡献了一次wrong answer,这次我把ascii字符表都考虑进去,然后就没问题了。这个故事告诫我们在编程处理问题的时候一定要注意输入数据的范围,面试中可以和面试官去确认数据范围,也能显得你比较严谨。
65 3
LeetCode 424. Longest Repeating Character Replacem
给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次。在执行上述操作后,找到包含重复字母的最长子串的长度。
114 0
LeetCode 424. Longest Repeating Character Replacem
LeetCode 409. Longest Palindrome
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
94 0
LeetCode 409. Longest Palindrome
LeetCode 395. Longest Substring with At Least K
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k 。输出 T 的长度。
107 0
LeetCode 395. Longest Substring with At Least K
|
Windows
LeetCode 388. Longest Absolute File Path
我们致力于寻找我们文件系统中文件的最长 (按字符的数量统计) 绝对路径。例如,在上述的第二个例子中,最长路径为 "dir/subdir2/subsubdir2/file2.ext",其长度为 32 (不包含双引号)。
96 0
LeetCode 388. Longest Absolute File Path
|
5月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
6月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
76 6
|
6月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
146 2
|
3月前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
99 1