Given a string s, partition s such that every substring of the partition is 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.

 

 

 求最小分割次数。
 
 
利用动态规划和一个boolean的二维数组记录是否是回文串。
 
public class Solution {
    public int minCut(String s) {
    
        int len = s.length();
        if( len <= 1)
            return 0;
        char[] word = s.toCharArray();
        int[] dp = new int[len];
        boolean[][] isPalindrome = new boolean[len][len];
        for( int i = 0;i<len;i++){
            int min = i;
            for( int j = 0;j<=i;j++){
                if( word[i] == word[j] && ( j+1>=i-1 || isPalindrome[j+1][i-1] )){
                    isPalindrome[j][i] = true;
                    min = j==0?0:Math.min(min,dp[j-1]+1);
                }
            }
            dp[i] = min;
        }
        
        return dp[len-1];
    }
    
}