Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
What is the length of the longest palindromic subsequence (not substring) of "ADCBEACD"?
- A. 3
- B. 5
- C. 8
- D. 1
Correct answer: B. 5
d[i][j] = d[i+1][j−1]+2 if s[i]=s[j], else max(d[i+1][j], d[i][j−1]) → 5 (equals LCS of s and its reverse). The longest palindromic substring has length 1.
More Coding Interview Problems questions
- Houses hold [15, 35, 2, 19, 25, 31, 12, 36] units of cash. You cannot rob two adjacent houses. What is the maximum amount you can rob?
- How many subsets (by index, so equal values at different positions count separately) of [4, 8, 1, 1, 5, 5] sum to exactly 8?
- What is the length of the longest palindromic subsequence (not substring) of "BDDCCBDB"?
- Houses hold [40, 38, 22, 30, 6, 2, 31, 18] units of cash. You cannot rob two adjacent houses. What is the maximum amount you can rob?
- How many subsets (by index, so equal values at different positions count separately) of [1, 7, 3, 2, 8, 9, 1, 7, 1] sum to exactly 9?
- What is the length of the longest palindromic subsequence (not substring) of "CDDDBCADE"?