Challenge: Longest Palindromic Subsequence

Find the longest palindromic subsequence in 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

s = "abdbca";

Sample output

The longest palindromic subsequence of the above string will be abdba, and the following would be the output:

result = 5;

Coding challenge

First, take a close look at this problem 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 solution provided in the solution section. Good luck!

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