【题目】给定一个字符串str,返回把str全部切成回文子串的最小分割数。
public class MinCut {
public static int minCut(String str) {
if (str == null || str.equals("")) {
return 0;
}
char[] chas = str.toCharArray();
int len = chas.length;
int[] dp = new int[len + 1];// dp数组
boolean[][] b = new boolean[len][len];
dp[len - 1] = -1;
for (int i = len - 1; i > -1; i--) {
dp[i] = Integer.MAX_VALUE;
for (int j = i; j < len; j++) {
if (chas[i] == chas[j] && (j - i < 2 || b[i + 1][j - 1])) {
b[i][j] = true;
dp[i] = Math.min(dp[i], dp[j + 1] + 1);
}
}
}
return dp[0];
}
}