Solution: Longest Palindromic Subsequence
Explore various approaches to solving the longest palindromic subsequence problem in detail.
We'll cover the following...
We'll cover the following...
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 ...