forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path132.Palindrome-Partitioning-II.cpp
44 lines (37 loc) · 1.01 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
class Solution {
public:
int minCut(string s)
{
int N = s.size();
auto isPal = vector<vector<bool>>(N,vector<bool>(N,0));
for (int len = 1; len<=N; len++)
for (int i=0; i<=N-len; i++)
{
int j = i+len-1;
// update isPal[i][j]
if (s[i]==s[j])
{
if (i+1>=j-1)
isPal[i][j] = true;
else
isPal[i][j] = isPal[i+1][j-1];
}
}
vector<int>dp(N, INT_MAX/2);
dp[0] = 1;
for (int i=1; i<N; i++)
{
for (int j=0; j<=i; j++)
{
if (isPal[j][i])
{
if (j==0)
dp[i] = 1;
else
dp[i] = min(dp[i], dp[j-1]+1);
}
}
}
return dp[N-1]-1;
}
};