Fewest character replacements so no two adjacent letters in a string match

Quick Overview

Find the minimum number of single-character replacements that leave a lowercase string with no two equal adjacent letters. It tests greedy reasoning on runs of repeated characters and a short proof that the count cannot be lowered.

Fewest character replacements so no two adjacent letters in a string match

Company: Canva

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a string `s` of lowercase English letters. You can reprogram any character of `s` by replacing it with any other lowercase English letter. Your goal is to make the string alternating, meaning no two adjacent characters are equal. Return the minimum number of character replacements needed to make `s` alternating. ### Function Signature ```python def min_reprogramming(s: str) -> int: ``` ### Rules - Each replacement changes the character at one position to a different lowercase letter. The new letter may already appear elsewhere in `s`. - The answer is the number of positions changed. A string of length 1 is already alternating. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists of lowercase English letters only. ### Examples ```text Input: s = "aabbcc" Output: 3 ``` Replacing the second character of each equal pair works, for example `"aabbcc"` becomes `"axbxcx"`. Each of the three pairs `"aa"`, `"bb"` and `"cc"` needs at least one change, so 3 is the minimum. ```text Input: s = "abcde" Output: 0 ``` The string is already alternating, so no replacements are needed. ```text Input: s = "aaaaa" Output: 2 ``` Changing positions 1 and 3 gives a string such as `"abaca"`. One change is not enough, because the pairs at positions 0-1 and 2-3 each need their own change.

Overview: Find the minimum number of single-character replacements that leave a lowercase string with no two equal adjacent letters. It tests greedy reasoning on runs of repeated characters and a short proof that the count cannot be lowered.

Canva logo
Canva
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s of lowercase English letters. You can reprogram any character of s by replacing it with any other lowercase English letter. Your goal is to make the string alternating, meaning no two adjacent characters are equal.

Return the minimum number of character replacements needed to make s alternating.

Function Signature

def min_reprogramming(s: str) -> int:

Rules

  • Each replacement changes the character at one position to a different lowercase letter. The new letter may already appear elsewhere in s .
  • The answer is the number of positions changed. A string of length 1 is already alternating.

Constraints

  • 1 <= len(s) <= 10^5
  • s consists of lowercase English letters only.

Examples

Input: s = "aabbcc"
Output: 3

Replacing the second character of each equal pair works, for example "aabbcc" becomes "axbxcx". Each of the three pairs "aa", "bb" and "cc" needs at least one change, so 3 is the minimum.

Input: s = "abcde"
Output: 0

The string is already alternating, so no replacements are needed.

Input: s = "aaaaa"
Output: 2

Changing positions 1 and 3 gives a string such as "abaca". One change is not enough, because the pairs at positions 0-1 and 2-3 each need their own change.