147. Strange Printer (Array Encoding)

HardArrayArray

A strange printer prints a string by printing a sequence of the same character starting and ending anywhere, overwriting existing characters. Return the minimum number of turns to print the given string. Input: a quoted string.

Input: A quoted string.

Output: Integer — minimum print turns.

Examples

Example 1
Input: "aaabbb"
Output: 2
Explanation: Print 'aaa' then 'bbb'.
Example 2
Input: "aba"
Output: 2
Explanation: Print 'aaa' then overwrite middle with 'b'.
Example 3
Input: "a"
Output: 1
Explanation: Single turn.

Constraints

Asked by

BloombergMicrosoftAmazonGoogle
Solve this problem in the editor →