132. Palindrome Partitioning II (DP)
Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.Last updated
Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.Last updated
class Solution {
public int minCut(String s) {
if (s == null || s.length() == 0) return 0;
int len = s.length();
int[] cuts = new int[len];
boolean[][] dp = new boolean[len][len];
for (int i = 0; i < len; i++) {
cuts[i] = i; //maximum cuts
for (int j = 0; j <= i; j++) {
if (s.charAt(j) == s.charAt(i) &&
(i - j <= 1 || dp[j + 1][i - 1])) {
dp[j][i] = true;
cuts[i] = j == 0 ? 0 : Math.min(cuts[i], cuts[j - 1] + 1);
}
}
}
return cuts[len - 1];
}
}