Skip to content

Commit 430d94a

Browse files
committed
test: 132 solution
c++
1 parent c6527ee commit 430d94a

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

problems/problems_132/Solution.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,25 @@ using json = nlohmann::json;
88
class Solution {
99
public:
1010
int minCut(string s) {
11-
11+
int n = s.length();
12+
vector<vector<bool>> is_palindrome(n, vector<bool>(n, false));
13+
for (int i = 0; i < n; ++i) {
14+
is_palindrome[i][i] = true;
15+
for (int j = i - 1; j >= 0; --j) {
16+
is_palindrome[j][i] = (s[i] == s[j]) && (i - j < 2 || is_palindrome[j + 1][i - 1]);
17+
}
18+
}
19+
20+
vector<int> cuts(n + 1, n);
21+
cuts[0] = 0;
22+
for (int i = 0; i < n; ++i) {
23+
for (int j = 0; j <= i; ++j) {
24+
if (is_palindrome[j][i]) {
25+
cuts[i + 1] = min(cuts[i + 1], cuts[j] + 1);
26+
}
27+
}
28+
}
29+
return cuts[n] - 1; // Subtract 1 because cuts[n] counts the number of partitions, not cuts
1230
}
1331
};
1432

problems/problems_132/testcase

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
["\"aab\"", "\"a\"", "\"ab\"", "\"abc\""]
2-
[1, 0, 1, 2]
1+
["\"aab\"", "\"a\"", "\"ab\"", "\"abc\"", "\"cabababcbc\""]
2+
[1, 0, 1, 2, 3]

0 commit comments

Comments
 (0)