664. Strange Printer LeetCode Solution

In this guide, you will get 664. Strange Printer LeetCode Solution with the best time and space complexity. The solution to Strange Printer problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Strange Printer solution in C++
  4. Strange Printer solution in Java
  5. Strange Printer solution in Python
  6. Additional Resources
664. Strange Printer LeetCode Solution image

Problem Statement of Strange Printer

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

The printer can only print a sequence of the same character each time.
At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.

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:

1 <= s.length <= 100
s consists of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n^3)
  • Space Complexity: O(n^2)

664. Strange Printer LeetCode Solution in C++

class Solution {
 public:
  int strangePrinter(string s) {
    const int n = s.length();
    vector<vector<int>> mem(n, vector<int>(n));
    return strangePrinter(s, 0, n - 1, mem);
  }

 private:
  // Returns the minimum number of turns to print s[i..j].
  int strangePrinter(const string& s, int i, int j, vector<vector<int>>& mem) {
    if (i > j)
      return 0;
    if (mem[i][j] > 0)
      return mem[i][j];

    // Print s[i].
    mem[i][j] = strangePrinter(s, i + 1, j, mem) + 1;

    for (int k = i + 1; k <= j; ++k)
      if (s[k] == s[i])
        mem[i][j] = min(mem[i][j], strangePrinter(s, i, k - 1, mem) +
                                       strangePrinter(s, k + 1, j, mem));

    return mem[i][j];
  }
};
/* code provided by PROGIEZ */

664. Strange Printer LeetCode Solution in Java

class Solution {
  public int strangePrinter(String s) {
    final int n = s.length();
    int[][] mem = new int[n][n];
    return strangePrinter(s, 0, n - 1, mem);
  }

  // Recursive helper method for strangePrinter
  private int strangePrinter(String s, int i, int j, int[][] mem) {
    if (i > j)
      return 0;
    if (mem[i][j] > 0)
      return mem[i][j];

    // Print s[i].
    mem[i][j] = strangePrinter(s, i + 1, j, mem) + 1;

    for (int k = i + 1; k <= j; k++)
      if (s.charAt(k) == s.charAt(i))
        mem[i][j] = Math.min(mem[i][j], strangePrinter(s, i, k - 1, mem) + //
                                            strangePrinter(s, k + 1, j, mem));

    return mem[i][j];
  }
}
// code provided by PROGIEZ

664. Strange Printer LeetCode Solution in Python

class Solution {
 public:
  int strangePrinter(string s) {
    if (s.empty())
      return 0;

    const int n = s.size();
    // dp[i][j] := the minimum number of turns to print s[i..j]
    vector<vector<int>> dp(n, vector<int>(n, n));

    for (int i = 0; i < n; ++i)
      dp[i][i] = 1;

    for (int j = 0; j < n; ++j)
      for (int i = j; i >= 0; --i)
        for (int k = i; k < j; ++k)
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] - (s[k] == s[j]));

    return dp[0][n - 1];
  }
};
# code by PROGIEZ

Additional Resources

See also  228. Summary Ranges LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.