Skip to content

Commit 142c98c

Browse files
Add input validation to longest palindromic subsequence
1 parent ff5be5b commit 142c98c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Dynamic-Programming/LongestPalindromicSubsequence.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
*/
88

99
export const longestPalindromeSubsequence = function (s) {
10+
if (typeof s !== 'string') {
11+
throw new TypeError('Input must be a string')
12+
}
13+
1014
const n = s.length
1115

12-
const dp = new Array(n)
13-
.fill(0)
14-
.map((item) => new Array(n).fill(0).map((item) => 0))
16+
if (n === 0) {
17+
return 0
18+
}
19+
20+
const dp = new Array(n).fill(0).map(() => new Array(n).fill(0))
1521

16-
// fill predefined for single character
22+
// single character palindromes
1723
for (let i = 0; i < n; i++) {
1824
dp[i][i] = 1
1925
}

0 commit comments

Comments
 (0)