PIXELBANKv9.1.0
Menu

Longest Palindromic Subsequence

Given a string s, return the length of the longest palindromic subsequence in s.

Example:

Input:
bbbab
Output:
4
Reasoning:
  • The input string s is bbbab, and we need to find the longest palindromic subsequence within it.
  • A palindromic subsequence can be formed by selecting characters that read the same backward as forward, such as bbbb or bbab is not a palindrome but bbb is, and single character b and a are also palindromes.
  • The longest palindromic subsequence in bbbab is bbbb or bbba is not valid since it is not a palindrome, but bbbb is, so we consider bbbb as the longest palindromic subsequence.
  • The length of the longest palindromic subsequence bbbb is 44, which is the output of the given input string.

Constraints:

  • 1 <= len(s) <= 1000
  • s consists of lowercase English letters
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.