Palindrome Partitioning II

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.

Solution

public class Solution {
  public int minCut(String s) {
        if (s == null || s.length() <= 1) return 0;

        int size = s.length();
        int[] dp = new int[size];

        dp[0] = 1;
        for (int i = 1; i < size; i++) {
            dp[i] = Integer.MAX_VALUE;
        }

        int[][] memory = new int[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                memory[i][j] = -1;
            }
        }

        for (int i = 1; i < size; i++) {
            for (int j = 0; j <= i; j++) {
                if (isPalindorm(s, j, i, memory)) {
                    if (j == 0) {
                        dp[i] = 1;
                    } else {
                        dp[i] = Math.min(dp[i], dp[j - 1] + 1);
                    }
                }
            }
        }

        return dp[size - 1] - 1;
    }

    private boolean isPalindorm(String s, int start, int end, int[][] memory) {
        if (memory[start][end] != -1) {
            if (memory[start][end] == 1) {
                return true;
            } else {
                return false;
            }
        }

        if (start >= end) return true;
        if (s.charAt(start) == s.charAt(end)) {
            boolean result = isPalindorm(s, start + 1, end - 1, memory);
            if (result) {
                memory[start][end] = 1;
            } else {
                memory[start][end] = 0;
            }

            return result;
        } else {
            memory[start][end] = 0;
            return false;
        }
    }
}

results matching ""

    No results matching ""