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.
For example, given s = "aab"
,
Return 1
since the palindrome partitioning ["aa","b"]
could
be produced using 1 cut.
class Solution {
public:
int minCut(string s) {
if(s.length()==0||s.length()==1) {
return 0;
}
vector<vector<int>> palindrome_map(s.length(), vector<int>(s.length()));
vector<int> cut_num_array(s.length() + 1);
for(int ii=s.length() - 1; ii >= 0;ii --) {
cut_num_array[ii] = s.length() - ii;
for(int jj = ii; jj < s.length(); jj ++) {
if(s[ii] == s[jj]) {
if(jj - ii < 2 || palindrome_map[ii + 1][jj - 1] == 1) {
palindrome_map[ii][jj] = 1;
cut_num_array[ii] = min(cut_num_array[ii], cut_num_array[jj + 1] + 1);
}
}
}
}
return cut_num_array[0] - 1;
}
};