#516

Longest Palindromic Subsequence

specialist · 630 · lc medium +30 · verified · 65.1% accepted · 10,331 likes · top 69%

play →

Description

Given a string s, determine the length of the longest subsequence within s that reads the same forwards and backwards. A subsequence is obtained by removing any characters from s without disturbing the relative order of the remaining characters.

Example 1:

Input: s = "bbbab"
Output: 4
Explanation: One possible longest palindromic subsequence is "bbbb".

Example 2:

Input: s = "cbbd"
Output: 2
Explanation: One possible longest palindromic subsequence is "bb".

Code

1
2
3