Skip to content

Commit 9e7727f

Browse files
authored
Create 132-palindrome-partitioning-ii.js
1 parent 7923726 commit 9e7727f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

132-palindrome-partitioning-ii.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
for (
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

Comments
 (0)