Challenge 6: 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 st = "abdbca"

Sample Output

5

Coding Exercise

Take a close look and design a step-by-step algorithm before jumping on 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.