This is similar to Longest Palindromic Substring, but it is hard to use the expanding idea cuz the subsequence is not continuous. We can try to use the dp idea as an initial:
Let $dp[i][j]$ to be the longest palindromic substring for $s[i, j]$, then:
$$ dp[i][j] = \begin{cases} dp[i+1][j-1] + 2, & \text{$s[i]=s[j]$} \\ \max\lbrace dp[i][j-1], dp[i+1][j] \rbrace, & \text{$s[i] \neq s[j]$} \\ 1, &\text{$i=j$} \\ 0, &\text{$i>j$} \end{cases} $$
|
|
Corner Cases
There are two corner cases as listed above, $i=j$ and $i<j$, we need to initialize them first.
i\j | 0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|---|
0 | 1 | a | c | f | j |
1 | 0 | 1 | b | e | i |
2 | 0 | 1 | d | h | |
3 | 0 | 1 | g | ||
4 | 0 | 1 |
|
|
Backtracking Solution
WIP