Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
What is the length of the longest palindromic subsequence (not substring) of "BDDCCBDB"?
- A. 12
- B. 6
- C. 5
- D. 9
Correct answer: B. 6
d[i][j] = d[i+1][j−1]+2 if s[i]=s[j], else max(d[i+1][j], d[i][j−1]) → 6 (equals LCS of s and its reverse). The longest palindromic substring has length 3.
More Coding Interview Problems questions
- 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"?
- Houses hold [15, 30, 28, 6, 27, 12, 20, 20] 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, 2, 9, 6, 2, 7, 6] sum to exactly 15?
- What is the length of the longest palindromic subsequence (not substring) of "DDCBAEBDC"?