Skip to content

Commit 553961b

Browse files
authored
Create 1312-minimum-insertion-steps-to-make-a-string-palindrome.js
1 parent 6426abf commit 553961b

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const minInsertions = function (s) {
6+
const dp = [...Array(501)].map((x) => Array(501).fill(0))
7+
const N = s.length
8+
for (let i = N - 1; i >= 0; --i)
9+
for (let j = i + 1; j <= N; ++j)
10+
if (s[i] == s[j - 1]) dp[i][j] = dp[i + 1][j - 1]
11+
else dp[i][j] = 1 + Math.min(dp[i + 1][j], dp[i][j - 1])
12+
return dp[0][N]
13+
}

0 commit comments

Comments
 (0)