Competitive Programming簡単

Longest Palindromic Subsequence

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

問題提起

簡単

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.

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

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

解決する準備はできましたか?

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

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。