Break Adjacent Duplicate Characters

Quick Overview

Practice counting the minimum replacements required to break adjacent duplicates across many lowercase words.

Break Adjacent Duplicate Characters

Company: IBM

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

# Break Adjacent Duplicate Characters For every input word, find the minimum number of single-character replacements needed so no two adjacent characters are equal. A replacement may use any lowercase letter. ### Function Signature ```text min_adjacent_replacements(words: list[str]) -> list[int] ``` ### Valid Input Domain Words is nonempty. Every word contains only lowercase ASCII letters and has length at least one. ### Exact Output Semantics Return one integer per word in the original order. Each integer is the minimum replacement count. A replacement can always choose a letter different from both neighbors because the alphabet has at least three letters; no modified strings are returned. ### Constraints - 1 <= words.length <= 100. - 1 <= words[i].length <= 100,000. - Sum of word lengths <= 1,000,000. ### Public Examples #### Example 1 Input: words = ["ab", "aab", "abb", "abab", "abaaabe"] Output: [0, 1, 1, 0, 1] Only one replacement is needed in each word containing a repeated adjacent run. #### Example 2 Input: words = ["aaaa", "a", "aabbcc"] Output: [2, 0, 3] A run of four equal letters needs two replacements, while each run of two in the last word needs one. ### Hints - Treat each maximal run of one repeated character independently. - Within a run, one replacement can break at most two adjacent equal pairs.

Quick Answer: Practice counting the minimum replacements required to break adjacent duplicates across many lowercase words.

|Home/Coding & Algorithms/IBM
IBM logo
IBM
Aug 28, 2026
mediumSoftware EngineerOnline AssessmentCoding & Algorithms
2
0

Break Adjacent Duplicate Characters

For every input word, find the minimum number of single-character replacements needed so no two adjacent characters are equal. A replacement may use any lowercase letter.

Function Signature

min_adjacent_replacements(words: list[str]) -> list[int]

Valid Input Domain

Words is nonempty. Every word contains only lowercase ASCII letters and has length at least one.

Exact Output Semantics

Return one integer per word in the original order. Each integer is the minimum replacement count. A replacement can always choose a letter different from both neighbors because the alphabet has at least three letters; no modified strings are returned.

Constraints

  • 1 <= words.length <= 100.
  • 1 <= words[i].length <= 100,000.
  • Sum of word lengths <= 1,000,000.

Public Examples

Example 1

Input: words = ["ab", "aab", "abb", "abab", "abaaabe"]

Output: [0, 1, 1, 0, 1]

Only one replacement is needed in each word containing a repeated adjacent run.

Example 2

Input: words = ["aaaa", "a", "aabbcc"]

Output: [2, 0, 3]

A run of four equal letters needs two replacements, while each run of two in the last word needs one.

Hints

  • Treat each maximal run of one repeated character independently.
  • Within a run, one replacement can break at most two adjacent equal pairs.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...