Solution: Longest Palindromic Subsequence
Explore dynamic programming techniques to find the longest palindromic subsequence in a string. Understand brute force, memoization, and bottom-up approaches, including how to optimize recursive calls and use lookup tables, while analyzing time and space complexity for each solution.
Solution 1: Brute force
Explanation
By now, you must have noticed a pattern in the way we approach dynamic programming problems.
In this brute-force solution, we do the following:
- If the element at the beginning and the end are the same, we increase our count by two and make a recursive call for the remaining sequence (lines 22–23).
- Otherwise, we skip the element either from the beginning or the end to make two recursive calls for the remaining subsequence. After that, we return the greater result (lines 26–27).
Time complexity
The time complexity of the above algorithm is exponential , where is the length of the input sequence. The space complexity is , which is the maximum number of ...