Longest Palindromic Substring

  • tags: [palindrome]

Question

  1. Given a string S, find the longest palindromic substring in S.
  2. You may assume that the maximum length of S is 1000,
  3. and there exists one unique longest palindromic substring.
  4. Example
  5. Given the string = "abcdzdcab", return "cdzdc".
  6. Challenge
  7. O(n2) time is acceptable. Can you do it in O(n) time.

題解1 - 窮舉搜索(brute force)

最簡單的方案,窮舉所有可能的子串,判斷子串是否為回文,使用一變數記錄最大回文長度,若新的回文超過之前的最大回文長度則更新標記變數並記錄當前回文的起止索引,最後返回最長回文子串。

Python

  1. class Solution:
  2. # @param {string} s input string
  3. # @return {string} the longest palindromic substring
  4. def longestPalindrome(self, s):
  5. if not s:
  6. return ""
  7. n = len(s)
  8. longest, left, right = 0, 0, 0
  9. for i in xrange(0, n):
  10. for j in xrange(i + 1, n + 1):
  11. substr = s[i:j]
  12. if self.isPalindrome(substr) and len(substr) > longest:
  13. longest = len(substr)
  14. left, right = i, j
  15. # construct longest substr
  16. result = s[left:right]
  17. return result
  18. def isPalindrome(self, s):
  19. if not s:
  20. return False
  21. # reverse compare
  22. return s == s[::-1]

C++

  1. class Solution {
  2. public:
  3. /**
  4. * @param s input string
  5. * @return the longest palindromic substring
  6. */
  7. string longestPalindrome(string& s) {
  8. string result;
  9. if (s.empty()) return s;
  10. int n = s.size();
  11. int longest = 0, left = 0, right = 0;
  12. for (int i = 0; i < n; ++i) {
  13. for (int j = i + 1; j <= n; ++j) {
  14. string substr = s.substr(i, j - i);
  15. if (isPalindrome(substr) && substr.size() > longest) {
  16. longest = j - i;
  17. left = i;
  18. right = j;
  19. }
  20. }
  21. }
  22. result = s.substr(left, right - left);
  23. return result;
  24. }
  25. private:
  26. bool isPalindrome(string &s) {
  27. int n = s.size();
  28. for (int i = 0; i < n; ++i) {
  29. if (s[i] != s[n - i - 1]) return false;
  30. }
  31. return true;
  32. }
  33. };

Java

  1. public class Solution {
  2. /**
  3. * @param s input string
  4. * @return the longest palindromic substring
  5. */
  6. public String longestPalindrome(String s) {
  7. String result = new String();
  8. if (s == null || s.isEmpty()) return result;
  9. int n = s.length();
  10. int longest = 0, left = 0, right = 0;
  11. for (int i = 0; i < n; i++) {
  12. for (int j = i + 1; j <= n; j++) {
  13. String substr = s.substring(i, j);
  14. if (isPalindrome(substr) && substr.length() > longest) {
  15. longest = substr.length();
  16. left = i;
  17. right = j;
  18. }
  19. }
  20. }
  21. result = s.substring(left, right);
  22. return result;
  23. }
  24. private boolean isPalindrome(String s) {
  25. if (s == null || s.isEmpty()) return false;
  26. int n = s.length();
  27. for (int i = 0; i < n; i++) {
  28. if (s.charAt(i) != s.charAt(n - i - 1)) return false;
  29. }
  30. return true;
  31. }
  32. }

源碼分析

使用left, right作為子串的起止索引,用於最後構造返回結果,避免中間構造字串以減少開銷。

複雜度分析

窮舉所有的子串,O(C_n^2) = O(n^2), 每次判斷字符串是否為回文,複雜度為 O(n), 故總的時間複雜度為 O(n^3). 故大數據集下可能 TLE. 使用了substr作為臨時子串,空間複雜度為 O(n).

題解2 - 動態規劃(dynamic programming)

要改善效率,可以觀察哪邊有重複而冗餘的計算,例如已知”bab”為回文的情況下,若前後各加一個相同的字元,”cbabc”,當然也是回文。
因此可以使用動態規劃,將先前的結果儲存起來,假設字串s的長度為n,我們創建一個(n\times n)的bool值矩陣PP[i, j], i<= j表示由[s_i, …, s_j]構成的子串是否為回文。就可以得到一個與子結構關係

P[i, j] = P[i+1, j-1] AND s[i] == s[j]

而基本狀態為

P[i, i] = true



P[i, i+1] = (s[i] == s[i+1])

因此可以整理成程式碼如下

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

複雜度分析

仍然是兩層迴圈,但每次迴圈內部只有常數次操作,因此時間複雜度是O(n^2),另外空間複雜度是O(n^2)

題解 3 - Manacher’s Algorithm

Reference