We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7923726 commit 9e7727fCopy full SHA for 9e7727f
132-palindrome-partitioning-ii.js
@@ -0,0 +1,25 @@
1
+/**
2
+ * @param {string} s
3
+ * @return {number}
4
+ */
5
+const minCut = function(s) {
6
+ const n = s.length
7
+ const cut = new Array(n + 1).fill(0)
8
+ for (let i = 0; i <= n; i++) cut[i] = i - 1
9
+ for (let i = 0; i < n; i++) {
10
+ for (
11
+ let j = 0;
12
+ i - j >= 0 && i + j < n && s[i - j] == s[i + j];
13
+ j++ // odd length palindrome
14
+ )
15
+ cut[i + j + 1] = Math.min(cut[i + j + 1], 1 + cut[i - j])
16
+
17
18
+ let j = 1;
19
+ i - j + 1 >= 0 && i + j < n && s[i - j + 1] == s[i + j];
20
+ j++ // even length palindrome
21
22
+ cut[i + j + 1] = Math.min(cut[i + j + 1], 1 + cut[i - j + 1])
23
+ }
24
+ return cut[n]
25
+}
0 commit comments