Skip to content

Instantly share code, notes, and snippets.

@songouyang
Last active November 29, 2017 14:43
Show Gist options
  • Save songouyang/a3260cc9cab6451630e2390cbdd232f4 to your computer and use it in GitHub Desktop.
Save songouyang/a3260cc9cab6451630e2390cbdd232f4 to your computer and use it in GitHub Desktop.
132. Palindrome Partitioning II
class Solution {
public:
int minCut(string s) {
unsigned long t = s.size();
vector<vector<bool>> p(t, vector<bool>(t, false));
vector<int> dp(t, INT_MAX);
for (int i = 0; i < t; ++i) {
for (int j = i; j >= 0; --j) {
if ((i - j <= 1 || p[j + 1][i - 1]) && s[i] == s[j]) {
p[j][i] = true;
}
if (p[j][i]) {
if (j == 0) {
dp[i] = 0;
} else {
dp[i] = min(dp[i], dp[j - 1] + 1);
}
}
}
}
return dp[t - 1];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment