[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. 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:
- P[i][i] = 1 (each character itself is palindromic);
- P[i][i + 1] = s[i] == s[j] (two neighboring characters are palindromic if they are the same);
- 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 }