Longest Palindromic Subsequence in JavaScript
Last Updated :
30 Jan, 2024
A Longest Palindromic Subsequence in JavaScript refers to the longest sequence of characters within a string that reads the same backward and forward. It’s a non-contiguous subsequence, and the goal is to find the maximum length of such subsequences in a given input string.
Example:
Input: str = “GEEKSFORGEEKS”
Output: EEKEE, 5
Explanation: The longest palindromic subsequence we can get is of length 5.
There are more than 1 palindromic subsequences of length 5, for example: EEKEE, EESEE, EEFEE, …etc.
Below approaches can be used to achieve this task
Using dynamic programming
In this approach, The palindromicSubsequence function uses dynamic programming to find the longest palindromic subsequence in a given string. It fills the array by comparing characters and their corresponding subsequences. The final result is the longest palindromic subsequence found.
Example: The below code example implements dynamic programming to find the longest palindromic subsequence.
Javascript
function palindromicSubsequence(str) {
const n = str.length;
const dp = Array.from({ length: n },
() => Array(n).fill( '' ));
for (let i = 0; i < n; i++) {
dp[i][i] = str[i];
}
for (let cl = 2; cl <= n; cl++) {
for (let index = 0; index + cl - 1 < n; index++) {
const endIndex = index + cl - 1;
const currentChar = str[index];
const endChar = str[endIndex];
if (currentChar === endChar) {
dp[index][endIndex] =
cl === 2 ? currentChar + endChar :
currentChar + dp[index + 1][endIndex - 1]
+ endChar;
} else {
const leftSubstring = dp[index][endIndex - 1];
const bottomSubstring = dp[index + 1][endIndex];
dp[index][endIndex] = leftSubstring.length >
bottomSubstring.length
? leftSubstring : bottomSubstring;
}
}
}
return dp[0][n - 1];
}
const inputStr = "GEEKSFORGEEKS" ;
const result = palindromicSubsequence(inputStr);
console.log(`Longest Palindromic Subsequence: ${result}`);
console.log(`Longest Palindromic Subsequence Length: ${result.length}`);
|
Output
Longest Palindromic Subsequence: EEGEE
Longest Palindromic Subsequence Length: 5
Using Recursive method
In this approach, we will use recursion to find the Longest Palindromic Subsequence (LPS) of a given string seq. It explores all possible substrings and returns the longest palindromic subsequence found.
Example: The below code explores all possible palindromic subsequences of the input string using the recursive approach.
Javascript
function max(x, y) {
return x > y ? x : y;
}
function lps(seq, i, j) {
if (i === j) {
return { length: 1, subsequence: seq[i] };
}
if (i + 1 === j && seq[i] === seq[j]) {
return {
length: 2, subsequence: seq[i]
+ seq[j]
};
}
if (seq[i] === seq[j]) {
const innerResult = lps(seq, i + 1, j - 1);
return {
length: innerResult.length + 2,
subsequence: seq[i] + innerResult.subsequence
+ seq[j],
};
}
const leftResult = lps(seq, i, j - 1);
const rightResult = lps(seq, i + 1, j);
return leftResult.length > rightResult.length ?
leftResult : rightResult;
}
const seq = "GEEKSFORGEEKS" ;
const n = seq.length;
const result = lps(seq.split( "" ), 0, n - 1);
console.log
( "Longest Palindromic Subsequence: " , result.subsequence);
console.log
( "Longest Palindromic Subsequence Length: " , result.subsequence.length);
|
Output
Longest Palindromic Subsequence: EEGEE
Longest Palindromic Subsequence Length: 5
Share your thoughts in the comments
Please Login to comment...