Leetcode 132 Solution
This article provides solution to leetcode question 132 (palindrome-partitioning-ii)
Access this page by simply typing in "lcs 132" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/palindrome-partitioning-ii
Solution
class Solution {
public:
int minCut(string s) {
if (s.size() == 0)
return 0;
vector<int> dp(s.size(), INT_MAX);
bool ispar[s.size()][s.size()];
memset((void*)ispar, 0, s.size() * s.size());
for (int j = 0; j < s.size(); j++)
{
for (int i = 0; i <= j; i++)
{
if (s[i] == s[j])
ispar[i][j] = j - 1 <= i + 1 || ispar[i + 1][j - 1];
}
}
for (int i = 0; i < s.size(); i++)
{
for (int j = 0; j <= i; j++)
{
if (ispar[j][i])
dp[i] = min(dp[i], (j > 0 ? dp[j - 1] + 1 : 1));
}
}
return dp.back() - 1;
}
};