#664

Strange Printer

expert · 1170 · lc hard +32 · verified · 60.9% accepted · 2,768 likes · top 60%

Description

A strange printer works in turns: each turn it prints a run of a single repeated character over any contiguous range of the output, overwriting whatever was there. Given a target string s, find the minimum number of turns needed to produce 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'.

Code

1
2
3