Solution: Longest Palindromic Substring
Let’s solve the Longest Palindromic Substring problem using the Dynamic Programming pattern.
Statement#
Given a string s, return the longest palindromic substring in s.
Constraints
-
s.length -
sconsist of only digits and English letters.
Solution#
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which to follow based on considerations, such as time complexity and implementation constraints.
Naive approach#
A naive approach to this problem is to find all possible substrings and select one longest palindromic substring. For example, consider the string “deed”, which contains 10 substrings: “d”, “e”, “e”, “d”, “de”, “ee”, “ed”, “dee”, “eed”, and “deed”. Out of these 10 substrings, six are palindromes: “d”, “e”, “e”, “d”, “ee”, and “deed”. Among these palindromes, the longest palindromic substring is “deed”.
We get the required result, but at what cost? Since we’re checking every possible substring, the total number of substrings in a string of length
Optimized approach using dynamic programming#
If we look at the example above, we notice that any substring of length dp, of size dp[i][j] will store whether the string s[i..j] is a palindromic substring. If the cell dp[i][j] holds the result of the earlier computation, we will utilize it in the
Create a resultant array,
res, to store the starting and ending indexes of the longest palindromic substring. Initialize it withdepicting that the longest palindromic substring seen so far is the first character of the input string. By definition, any string of length is always a palindrome. Initialize a lookup table,
dp, with FALSE.Base case 1: The diagonal in the lookup table is populated with TRUE, because any cell in the diagonal corresponds to a substring of length
, and any string of length is always a palindrome. Base case 2: We check whether all two-letter substrings are palindromes and update the
resanddpaccordingly. We do this by iterating over the string, comparings[i]ands[i+1], and storing the result atdp[i][i+1]. After that, we also updateresif the value ofdp[i][i+1]is TRUE, which tells us that the two-letter substring was a palindrome.After these base cases, we check all substrings of lengths greater than
. However, we only compare the first and the last characters. The rest of the string is checked using the lookup table. For example, for a given string “zing”, we want to check whether “zin” is a palindrome. We’ll only compare ‘z’ and ‘n’ and check the value of dp[1][1], which will tell whether the remaining string “i”, represented bys[1..1], is a palindrome. We’ll take the logical AND of these two results and store it atdp[0][2]because “zin” is represented by the substrings[0..2]. This way, we’ll avoid redundant computations and check all possible substrings using the lookup table.
Let’s look at the following illustration to get a better understanding of the solution:
1 of 17
2 of 17
3 of 17
4 of 17
5 of 17
6 of 17
7 of 17
8 of 17
9 of 17
10 of 17
11 of 17
12 of 17
13 of 17
14 of 17
15 of 17
16 of 17
17 of 17
Let's implement the algorithm as discussed above:
Solution summary#
To recap, the solution to this problem can be summarized as follows:
Initialize a resultant array with
to store the starting and ending indexes of the longest palindromic substring. Initialize a lookup table with FASLE except for the diagonal, which is initialized with TRUE because any one-letter string is always a palindrome.
Check all two-letter substrings and update the resultant array and the lookup table accordingly.
After these base cases, we check all possible substrings of lengths greater than
. However, it only compares the first and last characters, and the rest of the substring is checked using the lookup table. Whenever a palindromic substring is found, the resultant array and the lookup table are updated accordingly.
After checking all possible substrings, the algorithm terminates and returns the longest palindromic substrings based on the indexes in the resultant array.
Time complexity#
The time complexity of this algorithm is
Space complexity#
The space complexity of this algorithm is
Longest Palindromic Substring
Partition Equal Subset Sum