19. Longest Palindromic Subsequence
The problem can be found at the following link: Question Link
My Approach
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
checkfunction is a recursive auxiliary function that receives the current indicesiandjof the original stringAand the reversed stringrev, respectively. It also takes the lengthnof the string and a 2D vectordpfor memoization as parameters.The base case of the recursion is when either
iorjreaches the end of the string. In that case, the function returns 0.If the result for the current indices
iandjis already computed and stored indp, it is directly returned.If the characters at indices
iandjare 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
ibut incrementedjand vice versa, and store the maximum of the three results indp[i][j].Finally, the function returns the result stored in
dp[0][0], which represents the length of the longest palindromic subsequence.
Time and Auxiliary Space Complexity
Time Complexity: Each recursive call reduces the problem size by 1 until
iorjreaches the end of the string. Hence, the time complexity can be considered asO(n^2), wherenis the length of the string.Auxiliary Space Complexity: Since memoization array
dp, which is of sizen x n. Therefore, the auxiliary space complexity isO(n^2).
Code (C++)
class Solution {
public:
int check(int i, int j, int n, string& A, string& rev, vector<vector<int>>& dp) {
if (i == n || j == n)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
if (A[i] == rev[j]) {
dp[i][j] = 1 + check(i + 1, j + 1, n, A, rev, dp);
}
dp[i][j] = max(dp[i][j], check(i, j + 1, n, A, rev, dp));
dp[i][j] = max(dp[i][j], check(i + 1, j, n, A, rev, dp));
return dp[i][j];
}
int longestPalinSubseq(string A) {
int n = A.size();
string rev = A;
reverse(rev.begin(), rev.end());
vector<vector<int>> dp(n, vector<int>(n, -1));
return check(0, 0, n, A, rev, dp);
}
};Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated
Was this helpful?