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.
package leetCode; /** * Created by lxw, liwei4939@126.com on 2018/3/26. */ public class L132_PalindromePartition { public int minCut(String s) { if (s == null || s.equals("")){ return 0; } char[] chas = s.toCharArray(); int len = chas.length; int[] dp = new int[len+1]; dp[len] = -1; boolean[][] p = new boolean[len][len]; for (int i = len-1; i >= 0; i--){ dp[i] = Integer.MAX_VALUE; for (int j = i; j < len; j++){ if (chas[i] == chas[j] && (j-i < 2 || p[i+1][j-1])){ p[i][j] = true; dp[i] = Math.min(dp[i], dp[j+1] + 1); } } } return dp[0]; } }