-
Notifications
You must be signed in to change notification settings - Fork 22
/
132.palindrome-partitioning-ii.cpp
63 lines (59 loc) · 1.48 KB
/
132.palindrome-partitioning-ii.cpp
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
// Tag: String, Dynamic Programming
// Time: O(N^2)
// Space: O(N^2)
// Ref: -
// Note: -
// Given a string s, partition s such that every substring of the partition is a palindrome.
// Return the minimum cuts needed for a palindrome partitioning of s.
//
// Example 1:
//
// Input: s = "aab"
// Output: 1
// Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
//
// Example 2:
//
// Input: s = "a"
// Output: 0
//
// Example 3:
//
// Input: s = "ab"
// Output: 1
//
//
// Constraints:
//
// 1 <= s.length <= 2000
// s consists of lowercase English letters only.
//
//
class Solution {
public:
int minCut(string s) {
int n = s.size();
vector<vector<bool>> is_palindrome(n, vector<bool>(n, false));
for (int i = 0; i < n; i++) {
expand(s, i, i, is_palindrome);
expand(s, i, i + 1, is_palindrome);
}
vector<int> dp(n + 1, INT_MAX);
dp[0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
if (is_palindrome[j][i - 1]) {
dp[i] = min(dp[i], dp[j] + 1);
}
}
}
return dp[n] - 1;
}
void expand(string &s, int left, int right, vector<vector<bool>> &is_palindrome) {
while (left >=0 && right < s.size() && s[left] == s[right]) {
is_palindrome[left][right] = true;
left--;
right++;
}
}
};