HardStringDynamic Programming

Strange Printer

LeetCode
1 approach, code in all languages

A peculiar printer can only perform one kind of operation: in a single action it prints a solid run of one repeated character, and that run overwrites whatever characters occupied those positions before.

Given a target string s, you want to reproduce it exactly. Each action lays down a contiguous block of identical letters, possibly covering characters printed earlier.

Return the fewest number of printing actions required to end up with the string s.

Example 1

Input: s = "aaabbb"

Output: 2

Print "aaa" first, then print "bbb" to the right; two actions suffice.

Example 2

Input: s = "aba"

Output: 2

Print "aaa" as one run, then overwrite the middle character with "b" to get "aba".

Constraints

  • 1 <= s.length <= 100
  • s consists of lowercase English letters.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now