1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
public class PalindromePartitioningII { public int minCut(String s) { if (s == null || s.length() < 2) { return 0; } int l = s.length(); boolean[][] map = new boolean[l][l]; int[] cut = new int[l]; for (int i = 0; i < l; i++) { cut[i] = i; for (int j = 0; j <= i; j++) { if (s.charAt(i) == s.charAt(j) && (i - j <= 1 || map[j + 1][i - 1])) { map[j][i] = true; if (j > 0) { cut[i] = Math.min(cut[i], cut[j - 1] + 1); } else { cut[i] = 0; } } } } return cut[l - 1]; } @Test public void test() { System.out.println(minCut("efe")); } }
|