Minimize Overwriting Turns of a Single-Character Printer

Quick Overview

Minimize Overwriting Turns of a Single-Character Printer. A printer turn chooses one contiguous interval and writes the same chosen character throughout it, overwriting any existing characters.

Minimize Overwriting Turns of a Single-Character Printer

Company: Mastercard

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem A printer turn chooses one contiguous interval and writes the same chosen character throughout it, overwriting any existing characters. Return the fewest turns needed to produce s from an initially blank string of its length. ```cpp class Solution { public: int strangePrinter(string s); }; ``` ### Examples **Example 1:** ```text s = "cac" Output: 2 Explanation: Print ccc, then overwrite its middle with a. ``` **Example 2:** ```text s = "abcd" Output: 4 Explanation: Each distinct final character needs its own printing turn. ``` ### Constraints - Target size: 1..100 characters, all English lowercase. ### Clarifying Questions Allow overwriting and justify how separated equal characters can share a printing turn. ```hint Read the boundary condition Could two equal endpoint characters be supplied by one turn before the middle is repaired? ``` ### Contract considerations Allow overwriting and justify how separated equal characters can share a printing turn.

Overview: Minimize Overwriting Turns of a Single-Character Printer. A printer turn chooses one contiguous interval and writes the same chosen character throughout it, overwriting any existing characters.

Mastercard logo
Mastercard
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

A printer turn chooses one contiguous interval and writes the same chosen character throughout it, overwriting any existing characters. Return the fewest turns needed to produce s from an initially blank string of its length.

class Solution {
public:
    int strangePrinter(string s);
};

Examples

Example 1:

s = "cac"
Output: 2
Explanation: Print ccc, then overwrite its middle with a.

Example 2:

s = "abcd"
Output: 4
Explanation: Each distinct final character needs its own printing turn.

Constraints

  • Target size: 1..100 characters, all English lowercase.

Clarifying Questions Guidance

Allow overwriting and justify how separated equal characters can share a printing turn.

Contract considerations

Allow overwriting and justify how separated equal characters can share a printing turn.