Longest Palindromic Subsequence
MediumP2Coding
Return the length of the longest palindromic subsequence of s. A subsequence keeps the original order but may skip characters, and a palindrome reads the same forwards and backwards.
Example 1
- Input
- s = "bbbab"
- Output
- 4
- Why
- "bbbb" is a subsequence of length 4.
Example 2
- Input
- s = "cbbd"
- Output
- 2
- Why
- "bb" has length 2.
Example 3
- Input
- s = "a"
- Output
- 1
Constraints
- 1 <= s.length <= 1000
scontains lowercase English letters.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 11. ⌘Enter runs, ⌘⇧Enter submits.