Challenge: Longest Palindromic Subsequence

Let's write code to find the longest palindromic subsequence of a given string.

Problem statement

Given a string, find the length of its longest palindromic subsequence. In a palindromic subsequence, elements read the same backward and forward.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements

Input

A string.

Output

The length of the longest palindromic subsequence.

Sample input

string s = "abdbca"

Sample output

5

Coding exercise

Take a close look and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the hint and solution provided in the code tab. Good Luck!

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.