← All problems

664. Strange Printer

HardOpen on LeetCodeProblem statement

Problem Statement

664. Strange Printer

Hard


There is a strange printer with the following two special properties:

Given a string s, return the minimum number of turns the printer needed to print it.

 

Example 1:

Input: s = "aaabbb"
Output: 2
Explanation: Print "aaa" first and then print "bbb".

Example 2:

Input: s = "aba"
Output: 2
Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.

 

Constraints:

C++

Source file
class Solution {
public:
    int strangePrinter(string s) {
        s.erase(unique(s.begin(), s.end()), s.end());
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(n));
        // if substr.size()==1, then prints reqd: 1;
        for(int i=0; i<n; ++i)
            dp[i][i] = 1;

        for(int i=1; i<n; ++i){
            for(int j=0; j<n-i; ++j){
                int k = i + j;
                if(s[j]==s[k]){
                    dp[j][k] = dp[j][k-1];
                } else {
                    dp[j][k] = numeric_limits<int>::max();
                    for (int l = j; l < k; ++l) {
                        dp[j][k] = min(dp[j][k], dp[j][l] + dp[l + 1][k]);
                    }
                }
            }
        }
        return dp[0][n-1];
    }
};