-
Notifications
You must be signed in to change notification settings - Fork 4
/
palindrome-partitioning-ii.js
71 lines (65 loc) · 1.19 KB
/
palindrome-partitioning-ii.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var DEBUG = process.env.DEBUG;
var INF = Math.pow(2, 31);
function getl(s) {
var l = [0];
for (var i = 1; i <= s.length; i++) {
var j = i - 1;
l.push(0);
while (j !== 0) {
j = l[j];
if (s[j] === s[i - 1]) {
l[i] = j + 1;
break;
}
}
}
return l;
}
function buildJumpTo(s, pos) {
var t = s.substring(pos);
var l = getl(t);
var r = t.split('').reverse().join('');
var i = 0, j = 0;
while (i !== r.length) {
if (r[i] === t[j]) {
i++; j++;
} else {
if (j === 0) {
i++;
} else {
j = l[j];
}
}
}
var result = [];
while (j) {
result.push(pos + j);
j = l[j];
}
return result;
}
/**
* @param {string} s
* @return {number}
*/
var minCut = function(s) {
var jumpTo = [];
var minC = [];
for (var i = 0; i < s.length; i++) {
minC.push(INF);
}
minC.push(-1);
for (var i = s.length - 1; i >= 0; i--) {
jumpTo = buildJumpTo(s, i);
jumpTo.forEach(next => minC[i] = Math.min(minC[i], minC[next] + 1));
}
return minC[0];
};
function test(f) {
[
["aab"]
].forEach(function (input) {
console.log(f.apply(undefined, input));
});
}
if (DEBUG) test(minCut);