-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
32 lines (31 loc) · 868 Bytes
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @param {string} s
* @return {number}
*/
var minCut = function (s) {
const isPalindrome = new Array(s.length);
for (let i = s.length - 1; i > -1; i--) {
isPalindrome[i] = new Array(s.length).fill(true);
for (let j = i + 1; j < s.length; j++) {
if (s[i] === s[j]) {
isPalindrome[i][j] = isPalindrome[i + 1][j - 1];
} else {
isPalindrome[i][j] = false;
}
}
}
const dp = new Array(s.length);
for (let i = 0; i < s.length; i++) {
if (isPalindrome[0][i]) {
dp[i] = 0;
} else {
dp[i] = i;
for (let j = 0; j < i; j++) {
if (isPalindrome[j + 1][i]) {
dp[i] = Math.min(dp[j] + 1, dp[i]);
}
}
}
}
return dp[s.length - 1];
};