132. Palindrome Partitioning II

Problem:

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.

Example:

Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.

Solutions:

class Solution {
public:
    int minCut(string s) {
        int len = s.length();
        vector<vector<bool>> dp(len, vector<bool>(len, false));
        for (int j = 0; j < len; ++j) {
            for (int i = 0; i <= j; ++i) {
                if (s[i] == s[j] && (j - i <= 2 || dp[i+1][j-1])) {
                    dp[i][j] = true;
                }
            }
        }

        vector<int> cache (len, INT_MAX);
        return helper(s, 0, dp, cache) - 1;
    }

    int helper(string& s, int start, vector<vector<bool>>& dp, vector<int>& cache) {
        if (start >= s.length())    return 0;
        if (cache[start] != INT_MAX)    return cache[start];
        int ret = INT_MAX;
        for (int i = start; i < s.length(); ++i) {
            if (dp[start][i]) {
                ret = min(ret, 1 + helper(s, i + 1, dp, cache)); 
            }
        }

        cache[start] = ret;                  
        return ret;
    }
};

results matching ""

    No results matching ""