Professional Documents
Culture Documents
Longest Palindromic Subsequence
Longest Palindromic Subsequence
SUBSEQUENCE
TEST TIME ON MINIMUM STACK
URL:
LONGEST PALINDROMIC SUBSEQUENCE
EXPLANATION
EXAMPLE
Consider S = “ABBDCACB”
S = “BEBEEED”
SOLUTION
The idea is to use recursion to solve this problem. The idea is to compare
the last character of the string X[i…j] with its first character. There are
two possibilities
1. If the string’s last character is the same as the first character,
include the first and last characters in palindrome and recur for the
remaining substring X[i+1, j-1].
2. If the last character of the string is different from the first
character, return the maximum of the two values we get by
1. Removing the last character and recursing for the remaining
substring X[i, j-1].
2. Removing the first character and recursing for the remaining
substring X[i+1, j].
LONGEST PALINDROMIC SUBSEQUENCE
SOLUTION
This yields the following recursive relation to finding the length of the
longest repeated subsequence of a sequence X:
LONGEST PALINDROMIC SUBSEQUENCE
PROGRAM
Sample IO
Input
ABBDCACB
Output
5
Time Complexity
O(2^n), where n is the length of the input string
LONGEST PALINDROMIC SUBSEQUENCE
class Main {
public static int findLongestPalindrome(String X, int i, int j) {
if (i > j) {
return 0;
}
if (i == j) {
return 1;
}
if (X.charAt(i) == X.charAt(j)) {
return findLongestPalindrome(X, i + 1, j - 1) + 2;
}
System.out.print(findLongestPalindrome(X, 0, n - 1));
}
}
LONGEST PALINDROMIC SUBSEQUENCE
Time complexity: O(n^2) - Quadratic time complexity due to the recursive nature of the
Space complexity: O(n^2) - Quadratic space complexity due to the memoization table storing
intermediate results.
LONGEST PALINDROMIC SUBSEQUENCE
IMPROVEMENT
In the previous attempt, the worst case happens when there is no repeated
character present in X (i.e., LPS length is 1)
Also each recursive call will end up in two recursive calls. This also
requires additional space for the call stack.
However, the LPS problem has an optimal substructure and also exhibits
overlapping subproblems.
LONGEST PALINDROMIC SUBSEQUENCE
IMPROVEMENT
IMPROVEMENT
PROGRAM
Sample IO
Input
ABBDCACB
Output
5
LONGEST PALINDROMIC SUBSEQUENCE
Time complexity: O(n^2) - Due to the recursive nature of the algorithm with memoization,
Space complexity: O(n^2) - Due to the storage of intermediate results in the memoization
The discussed methods only find the longest palindromic subsequence length
but does not print the longest palindromic subsequence itself
The idea is to find LCS of the given string with its reverse
Call LCS(X, reverse(X)) and the longest common subsequence will be the
longest palindromic subsequence.
LONGEST PALINDROMIC SUBSEQUENCE
PROGRAM
Sample IO
Input
ABBDCACB
Output
5
BCACB
LONGEST PALINDROMIC SUBSEQUENCE
Time complexity: O(n^2) - Quadratic time complexity due to the recursive nature of the
Space complexity: O(n^2) - Quadratic space complexity due to the memoization table storing
intermediate results.
INTERVIEW QUESTIONS
whether characters match or not. The final value in the array represents
4. How would you extend the algorithm to find the actual Longest
Palindromic Subsequence itself, not just its length?
Answer:In addition to storing the lengths in the 2D array, maintain another array
order.
INTERVIEW QUESTIONS
array since each computation depends only on the previous row's values. This
reduces the space complexity from O(n^2) to O(n), making the algorithm more memory-
efficient.
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra
https://codemithra.com/vit-helpde
sk/