[leetcode] 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.

Dynamic programming.

dp[i] stores the minimum cut of s.substr(0, i + 1).

o(n^3) in time, o(n) in space solution

class Solution {
public:
    int minCut(string s) {
        int dp[s.size()];
        if(s.size() <= 1) return 0;
        dp[0] = 0;
        for(int i = 1; i < s.size(); i++){
            dp[i] = s.size(); //max value
            for(int j = i; j > 0; j--){
                if(dp[j - 1] + 1 >= dp[i]) continue;
                string tmps = s.substr(j, i - j + 1);
                if(isPalindrome(tmps)){
                    dp[i] = dp[j - 1] + 1 < dp[i] ? dp[j - 1] + 1 : dp[i];
                }
            }
            //j == 0
            if(isPalindrome(s.substr(0, i + 1))){
                dp[i] = 0;
            }
        }
        return dp[s.size() - 1];
    }
    bool isPalindrome(string s){
        //o(n)
        //extreme case
        if(s.size() == 1) return true;
        int i, j;
        if(s.size() % 2 == 0){
            //even
            i = s.size() / 2 - 1;
            j = i + 1;
        }
        else{
            //odd
            i = s.size() / 2 - 1;
            j = i + 2;
        }
        while(i >= 0){
            if(s[i] != s[j]) return false;
            i--;
            j++;
        }
        return true;
    }    
};

Untitled

 

o(n^2) in time, o(n^2) in space solution.

Construct a palindrome matrix, o(n^2),

Find min cut, o(n^2)

class Solution {
public:
    int minCut(string s) {
        int dp[s.size()];
        bool matrix[s.size()][s.size()]; //is palindrome 
        if(s.size() <= 1) return 0;
        dp[0] = 0;
        
        memset(matrix, false, sizeof(matrix));
        for(int i = 0; i < s.size(); i++){
            matrix[i][i] = true;
        }
        //o(n^2)
        for(int i = 0; i < s.size(); i++){
            for(int j = i + 1; j < s.size(); j++){
                if(s[i] == s[j] && (j - i == 1 || matrix[i + 1][j - 1] == true)){
                    matrix[i][j] = true;
                }
            }
            for(int j = i - 1; j >= 0 ; j--){
                if(s[j] == s[i] && (i - j == 1 || matrix[j + 1][i - 1] == true)){
                    matrix[j][i] = true;
                }
            }
        }
        
        for(int i = 1; i < s.size(); i++){
            dp[i] = s.size(); //max value
            for(int j = i; j > 0; j--){
                if(dp[j - 1] + 1 >= dp[i]) continue;
                if(matrix[j][i] == true){
                    dp[i] = dp[j - 1] + 1 < dp[i] ? dp[j - 1] + 1 : dp[i];
                }
            }
            //j == 0
            if(matrix[0][i] == true){
                dp[i] = 0;
            }
        }
        return dp[s.size() - 1];
    }
};

 

Untitled

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.