Given A string s, partition s such that every substring of the partition are a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab" ,
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.
Problem Solving Ideas:
Because it is hard, the result of using the problem is definitely timed out, the topic needs to use the DP idea, open a boolean[][] array to calculate whether I-j is palindrome, the recurrence relationship is S.charat (j) = = S.charat (i) && Ispal[j + 1][i-1]) →ispal[j][i] = True, while dp[i] = Math.min (Dp[i], dp[j-1] + 1), the Java implementation is as follows:
public int mincut (String s) {int[] dp = new Int[s.length ()];for (int i = 0; i < dp.length; i++) dp[i] = I;boolean ispal[ [] = new Boolean[s.length ()][s.length ()];for (int i = 1; i < s.length (); i++) for (int j = i; j >= 0; j--) if (S.cha RAt (j) = = S.charat (i) && (j + 1 >= i-1 | | ispal[j + 1][i-1])) {Ispal[j][i] = true;dp[i] = j = = 0? 0:math. Min (Dp[i], dp[j-1] + 1);} return dp[dp.length-1]; }
Java for Leetcode palindrome partitioning II