Minimum Insertion Steps to Make a String Palindrome LeetCode solution in JavaScript

1
var minInsertions = function (s) {
2
const len = s.length;
3
const dp = new Array(len).fill(0).map(() => {
4
return new Array(len).fill(-1);
5
});
6

7
const compute = (i = 0, j = len - 1) => {
8
if (i >= j) return 0;
9

10
if (dp[i][j] != -1) return dp[i][j];
11

12
if (s[i] == s[j]) return compute(i + 1, j - 1);
13

14
return (dp[i][j] = Math.min(compute(i + 1, j), compute(i, j - 1)) + 1);
15
};
16
return compute();
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0