-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path132.palindrome-partitioning-ii.cpp
59 lines (57 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
/*
* @lc app=leetcode id=132 lang=cpp
*
* [132] Palindrome Partitioning II
*
* https://leetcode.com/problems/palindrome-partitioning-ii/description/
*
* algorithms
* Hard (27.88%)
* Total Accepted: 107.2K
* Total Submissions: 384.2K
* Testcase Example: '"aab"'
*
* 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:
*
*
* Input: "aab"
* Output: 1
* Explanation: The palindrome partitioning ["aa","b"] could be produced using
* 1 cut.
*
*
*/
class Solution {
public:
unordered_map<int, int> mm;
int callme(int start, vector<vector<bool>>& arr){
if(start == (int)arr.size()) return 0;
if(mm.find(start)!=mm.end())
return mm[start];
int ans = INT_MAX;
for(int i=start; i<(int)arr.size(); i++){
if(!arr[start][i]) continue;
ans = min(ans, 1 + callme(i+1, arr));
}
mm[start] = ans;
return ans;
}
int minCut(string s) {
mm.clear();
int n = s.size();
vector<vector<bool>> arr(n, vector<bool>(n));
for(int i=0; i<n; i++)
arr[i][i] = true;
for(int i=0; i+1<n; i++)
arr[i][i+1] = (s[i] == s[i+1]);
for(int k=2; k<n; k++)
for(int i=0; i+k<n; i++)
arr[i][i+k] = (s[i] == s[i+k]) && arr[i+1][i+k-1];
return max(0, callme(0, arr)-1);
}
};