Thursday, March 12, 2015

LeetCode Palindrome Partitioning II

DP is for recording the min cut from i to the end.
isPalindrome is for recording the palindrome between index i and j.

public int minCut(String s) {
       int len = s.length();
       int[] dp = new int[len+1];
       boolean[][] isPalindrome = new boolean[len][len];
     
       for (int i=len; i>=0; i--) {
           dp[i] = len-i;
       }
       for (int i=len-1; i>=0; i--) {
           for (int j=i; j<len; j++) {
               if (s.charAt(i)==s.charAt(j) && (j-i<2 ||isPalindrome[i+1][j-1])) {
                   isPalindrome[i][j]=true;
                   dp[i] = Math.min(dp[i], dp[j+1]+1);
               }
           }
       }
       return dp[0]-1;
    }

No comments:

Post a Comment