Description
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.
Solution
Python3
class Solution:
def strangePrinter(self, s: str) -> int:
s = [key for key, group in itertools.groupby(s)]
@cache
def go(i, j):
if i == j: return 1
res = inf
for k in range(i, j):
res = min(res, go(i, k) + go(k + 1, j))
return res - 1 if s[i] == s[j] else res
return go(0, len(s) - 1)