Skip to content

Commit

Permalink
longest-palindromic-subsequence
Browse files Browse the repository at this point in the history
  • Loading branch information
aolenevme authored Apr 14, 2023
1 parent 7172037 commit 4053861
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions array/longest-palindromic-subsequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
516. Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s.
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: s = "bbbab"
Output: 4
Explanation: One possible longest palindromic subsequence is "bbbb".
Example 2:
Input: s = "cbbd"
Output: 2
Explanation: One possible longest palindromic subsequence is "bb".
Constraints:
1 <= s.length <= 1000
s consists only of lowercase English letters.
*/

/**
* @param {string} s
* @return {number}
*/
var longestPalindromeSubseq = function (s) {
const length = s.length;
const memo = {};

const lps = (left, right) => {
const key = `${left},${right}`;

if (memo[key]) {
return memo[key];
}

if (left > right) {
return 0;
}

if (left === right) {
return 1;
}

if (s[left] === s[right]) {
memo[key] = lps(left + 1, right - 1) + 2;
} else {
memo[key] = Math.max(lps(left, right - 1), lps(left + 1, right));
}

return memo[key];
};

return lps(0, length - 1);
};

0 comments on commit 4053861

Please sign in to comment.