19. Longest Palindromic Subsequence
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
In order to determine the length of the longest palindromic subsequence within a given string, I used the concept of the longest common subsequence. This involved finding the longest common subsequence between the original string, denoted as A
, and its reverse, denoted as rev
. Let's find that out using recursion and memoization.
The check
function is a recursive auxiliary function that receives the current indices i
and j
of the original string A
and the reversed string rev
, respectively. It also takes the length n
of the string and a 2D vector dp
for memoization as parameters.
The base case of the recursion is when either i
or j
reaches the end of the string. In that case, the function returns 0.
If the result for the current indices i
and j
is already computed and stored in dp
, it is directly returned.
If the characters at indices i
and j
are the same, we increment the result by 1 and recursively call the function with the incremented indices.
We also recursively call the function with the same i
but incremented j
and vice versa, and store the maximum of the three results in dp[i][j]
.
Finally, the function returns the result stored in dp[0][0]
, which represents the length of the longest palindromic subsequence.
Time Complexity: Each recursive call reduces the problem size by 1 until i
or j
reaches the end of the string. Hence, the time complexity can be considered as O(n^2)
, where n
is the length of the string.
Auxiliary Space Complexity: Since memoization array dp
, which is of size n x n
. Therefore, the auxiliary space complexity is O(n^2)
.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.