add js solution for longestPalindromeSubseq

This commit is contained in:
Qi Jia
2021-07-17 14:37:00 -07:00
committed by GitHub
parent e70378fc62
commit 80dda7ac09

View File

@ -214,7 +214,29 @@ func longestPalindromeSubseq(s string) int {
} }
``` ```
Javascript
```javascript
const longestPalindromeSubseq = (s) => {
const strLen = s.length;
let dp = Array.from(Array(strLen), () => Array(strLen).fill(0));
for(let i = 0; i < strLen; i++) {
dp[i][i] = 1;
}
for(let i = strLen - 1; i >= 0; i--) {
for(let j = i + 1; j < strLen; j++) {
if(s[i] === s[j]) {
dp[i][j] = dp[i+1][j-1] + 2;
} else {
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
}
}
}
return dp[0][strLen - 1];
};
```
----------------------- -----------------------