-
Notifications
You must be signed in to change notification settings - Fork 0
/
strange_printer.py
38 lines (27 loc) · 913 Bytes
/
strange_printer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution:
def strangePrinter(self, s: str) -> int:
new_s = ""
prev = ""
for c in s:
if c == prev:
continue
new_s += c
prev = c
s, n = new_s, len(new_s)
memo = {}
def min_prints(start, end):
if start > end or end >= n:
return 0
if (start, end) in memo:
return memo[(start, end)]
if end == start:
return 1
ans = min_prints(start + 1, end) + 1
for ptr in range(start + 1, end + 1):
if s[start] == s[ptr]:
ans = min(ans, min_prints(start, ptr - 1) + min_prints(ptr + 1, end))
memo[(start, end)] = ans
return ans
return min_prints(0, n - 1)
print(Solution().strangePrinter("aa"))
print(Solution().strangePrinter("aabb"))