Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created September 18, 2017 22:27
Show Gist options
  • Save cixuuz/d24a6ab3fa29c794a0020d70dee46dbc to your computer and use it in GitHub Desktop.
Save cixuuz/d24a6ab3fa29c794a0020d70dee46dbc to your computer and use it in GitHub Desktop.
[132. Palindrome Partitioning II] #leetcode
class Solution {
public int minCut(String s) {
char[] c = s.toCharArray();
int n = c.length;
int[] cut = new int[n];
boolean[][] pal = new boolean[n][n];
for(int i = 0; i < n; i++) {
int min = i;
for(int j = 0; j <= i; j++) {
if(c[j] == c[i] && (j + 1 > i - 1 || pal[j + 1][i - 1])) {
pal[j][i] = true;
min = Math.min(min, j == 0 ? 0 : cut[j - 1] + 1);
}
}
cut[i] = min;
}
return cut[n - 1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment