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. Note: See the previous wrong answers
class Solution {
public:
int minCut(string s) {
int slen=s.size();
if (slen<=1) return 0;
vector<int> minc(slen+1,0);
for (int i=0; i<slen+1; i++)
{
minc[i]=i-1;
}
for (int i=0; i<slen; i++)
{
for (int j=0; i-j>=0&&i+j<slen&&s[i-j]==s[i+j]; j++)
{
minc[i+j+1]=min(minc[i+j+1],minc[i-j]+1);
}
for (int j=1; i-j+1>=0&&i+j<slen&&s[i-j+1]==s[i+j]; j++)
{
minc[i+j+1]=min(minc[i+j+1],minc[i-j+1]+1);
}
}
return minc[slen];
}
};
No comments:
Post a Comment