Grokking Dynamic Programming Patterns for Coding Interviews
Vote

0% completed

​

Longest Palindromic Subsequence

Problem Statement

Try it yourself

Problem Statement

Given a sequence, find the length of its Longest Palindromic Subsequence (LPS). 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.

Example 1:

Input: "abdbca"
Output: 5
Explanation: LPS is "abdba".

Example 2:

Input: = "cddpd"
Output: 3
Explanation: LPS is "ddd".

Example 3:

Input: = "pqr"
Output: 1
Explanation: LPS could be "p", "q" or "r".

Constraints:

  • 1 <= st.length <= 1000
  • sy consists only of lowercase English letters.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .
A

Anonymous

· 2 years ago

class Solution: def findLPSLength(self, st): def is_palindrom(s): i = 0 j = len(s) - 1 while i <= j: if s[i] != s[j]: return False i += 1 j -= 1 return True def dp(index, s, memo): if s in memo: return memo[s] if is_palindrom(s): return len(s) if index >= len(s): return 0 skip_char = dp(index + 1, s[:], memo) with_char = dp(index + 1, s[:index] + s[index+1:], memo) result = max(skip_char, with_char) memo[s] = result return result return dp(0, st, {})
A

Anonymous

· 4 years ago

Why does the bottom up solution here start with startIndex at n - 1 instead of 0, like in the Longest Common Substring/Subsequence problems in chapter 6?

Show 1 reply
A

Anonymous

· 5 years ago

For the memoisation tecgnique, isn’t the time complexity still 2^n? We still have 2^n function calls...

Show 1 reply

Reading Progress

0%


Vote for new content

On This Page

Problem Statement

Try it yourself