Skip to content
Prev Previous commit
Next Next commit
Add input validation to longest palindromic subsequence
  • Loading branch information
MayankSharma-2812 committed Jan 31, 2026
commit 142c98c9794fae795b7af120d6b9a51bc82c1b5b
14 changes: 10 additions & 4 deletions Dynamic-Programming/LongestPalindromicSubsequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
*/

export const longestPalindromeSubsequence = function (s) {
if (typeof s !== 'string') {
throw new TypeError('Input must be a string')
}

const n = s.length

const dp = new Array(n)
.fill(0)
.map((item) => new Array(n).fill(0).map((item) => 0))
if (n === 0) {
return 0
}

const dp = new Array(n).fill(0).map(() => new Array(n).fill(0))

// fill predefined for single character
// single character palindromes
for (let i = 0; i < n; i++) {
dp[i][i] = 1
}
Expand Down