Mid Coding

Palindrome Partitioning (minimum cuts)?

int MinCutPalindromePartition(string s) {

int n = s.Length;

bool[,] dp = new bool[n, n];

int[] cuts = new int[n];

for (int i = 0; i < n; i++) {

int minCuts = i;

for (int j = 0; j <= i; j++) {

if (s[j] == s[i] && (i - j < 2 || dp[j + 1, i - 1])) {

dp[j, i] = true;

minCuts = j == 0 ? 0 : Math.Min(minCuts, cuts[j - 1]

+ 1);

cuts[i] = minCuts;

return cuts[n - 1];

More from C# Programming Tutorial

All questions for this course