Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 516 Longest Palindromic Subsequence - Problem Statement

Entry problem

Given a string ss, find the length of the longest subsequence that reads the same forward and backward. Example: for s=s = "bbbab", the answer is 44 (the subsequence "bbbb").

This is your first interval DP problem. The important point: any palindromic subsequence on s[i..j]s[i..j] depends on whether s[i]=s[j]s[i] = s[j]. If they match, both characters can be part of the palindrome. If not, at least you must be skipped. Before reading on, think: what's the simplest base case? What happens when i=ji = j?