LeetCode 664, Hard. Topics: String, Dynamic Programming. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 26 parametrized pytest cases, and a playground notebook:
Problem
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.
Examples
Constraints
1 <= s.length <= 100
s consists of lowercase English letters.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026