Strange Printer - 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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "aba"
›
Output:
2
💡 Note:
First print 'a' at positions 0 and 2, then print 'b' at position 1. Total: 2 turns.
Example 2 — All Same Characters
$
Input:
s = "aaabbb"
›
Output:
2
💡 Note:
First print 'aaa' (positions 0-2), then print 'bbb' (positions 3-5). Total: 2 turns.
Example 3 — All Different
$
Input:
s = "abcd"
›
Output:
4
💡 Note:
Each character is different, so we need 4 separate printing operations.
Constraints
- 1 ≤ s.length ≤ 100
- s consists of lowercase English letters.
Visualization
Tap to expand
Understanding the Visualization
1
Input String
String 'aba' needs to be printed
2
Smart Printing
Print matching characters together
3
Minimum Turns
Find the optimal printing sequence
Key Takeaway
🎯 Key Insight: When characters at the ends match, we can print them together and solve the middle part separately, leading to optimal substructure for dynamic programming.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code