Strange Printer
1 approach, code in every language
Problem description
A peculiar printer can only perform one kind of operation: in a single action it prints a solid run of one repeated character, and that run overwrites whatever characters occupied those positions before.
Given a target string s, you want to reproduce it exactly. Each action lays down a contiguous block of identical letters, possibly covering characters printed earlier.
Return the fewest number of printing actions required to end up with the string s.
Example 1
Input: s = "aaabbb"
Output: 2
Print "aaa" first, then print "bbb" to the right; two actions suffice.
Example 2
Input: s = "aba"
Output: 2
Print "aaa" as one run, then overwrite the middle character with "b" to get "aba".
Constraints
1 <= s.length <= 100s consists of lowercase English letters.
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Sign Press Passes is our own problem on the technique behind Strange Printer: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free