Longest Palindromic Subsequence
Given a string s, return the length of the longest palindromic subsequence of s. A subsequence is formed by deleting zero or more characters without reordering the rest, and it is a palindrome if it reads the same forwards and backwards.
Open official problem prompt ↗Compute the length of the longest subsequence of s that is a palindrome, without needing to construct it.
Imagine matching the outermost bookends on a shelf: if the two end books are identical you keep both and recurse on the shelf between them; if not, you set one end aside and try the rest.
- Input
- s = "bbbab"
- Output
- 4
- Why
- The subsequence "bbbb" (dropping the 'a') is a palindrome of length 4.
1 <= s.length <= 1000s consists only of lowercase English letters