public class Solution {
public int minCut(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int length = s.length();
boolean[][] isPalindrome = new boolean[length][length];
int[] cut = new int[length];
for (int j = 0; j < length; j++) {
cut[j] = j;
for (int i = 0; i <= j; i++) {
if (s.charAt(i) == s.charAt(j) && (j - i <= 1 || isPalindrome[i + 1][j - 1])) {
isPalindrome[i][j] = true;
if (i == 0) {
cut[j] = 0;
} else {
cut[j] = Math.min(cut[j], cut[i - 1] + 1);
}
}
}
}
return cut[length - 1];
}
}