Competitive ProgrammingEasy

Longest Palindromic Subsequence

Detailed guide and Python implementation for the 'Longest Palindromic Subsequence' problem.

Problem Statement

Easy

Write a function lps(s) that finds the length of the longest palindromic subsequence in a string s. 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.

Constraints
  • 1 <= len(s) <= 1000
  • s consists of uppercase or lowercase English letters.

Examples

Example 1
Input
lps('BBABCBCAB')
Output
7
Explanation

The longest palindromic subsequence has length 7, e.g., 'BABCBAB'.

Example 2
Input
lps('GEEKSFORGEEKS')
Output
5
Explanation

The longest palindromic subsequence has length 5, e.g., 'EEKEE'.

Need a Hint?
Consider using Dynamic Programming-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.