Quick Overview

Compute the fewest character replacements needed to eliminate equal adjacent letters in each word. Handle repeated runs, run boundaries, empty words, and independent results without constructing the modified strings.

Minimize replacements needed to separate equal neighboring letters

Company: Headlands

Role: Quantitative Researcher

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

For each lowercase word, return the minimum number of character replacements needed so that no two adjacent characters are equal. A replacement may change one character to any other lowercase English letter. ### Function Signature ```python def minimum_replacements(words: list[str]) -> list[int]: ``` ### Rules Return one minimum count per word in input order. Words are independent. Do not return modified words. ### Constraints These bounds define this practice version. - `0 <= len(words) <= 100000`. Total character count is at most 1000000. - Each word contains only `a` through `z` and may be empty. ### Examples ```text Input: words = ["aab", "aaaa", "ababa", ""] Output: [1, 2, 0, 0] ``` One replacement breaks the initial `aa`. Two alternating replacements separate four equal letters. The final two words already meet the condition.

Overview: Compute the fewest character replacements needed to eliminate equal adjacent letters in each word. Handle repeated runs, run boundaries, empty words, and independent results without constructing the modified strings.

Read the full Headlands Quantitative Researcher interview experience this question came from

For each lowercase word, return the minimum number of character replacements needed so that no two adjacent characters are equal. A replacement may change one character to any other lowercase English letter. ### Function Signature ```python def minimum_replacements(words: list[str]) -> list[int]: ``` ### Rules Return one minimum count per word in input order. Words are independent. Do not return modified words. ### Constraints These bounds define this practice version. - `0 <= len(words) <= 100000`. Total character count is at most 1000000. - Each word contains only `a` through `z` and may be empty. ### Examples ```text Input: words = ["aab", "aaaa", "ababa", ""] Output: [1, 2, 0, 0] ``` One replacement breaks the initial `aa`. Two alternating replacements separate four equal letters. The final two words already meet the condition.

Constraints

  • `0 <= len(words) <= 100000`. Total character count is at most 1000000.
  • Each word contains only `a` through `z` and may be empty.

Examples

Input: (['aab', 'aaaa', 'ababa', ''],)

Expected Output: [1, 2, 0, 0]

Explanation: Even and odd-length runs contribute separately; an empty word contributes zero.

Input: (['baabb', 'z'],)

Expected Output: [2, 0]

Explanation: Two separate equal pairs each require a replacement.

Hints

  1. Within a run of identical letters, each disjoint adjacent pair must be broken.
  2. Different words have independent replacement counts.

Minimize replacements needed to separate equal neighboring letters

hardHeadlandsQuantitative Researcher

For each lowercase word, return the minimum number of character replacements needed so that no two adjacent characters are equal. A replacement may change one character to any other lowercase English letter.

Function Signature

def minimum_replacements(words: list[str]) -> list[int]:

Rules

Return one minimum count per word in input order. Words are independent. Do not return modified words.

Constraints

These bounds define this practice version.

  • 0 <= len(words) <= 100000. Total character count is at most 1000000.
  • Each word contains only a through z and may be empty.

Examples

Input: words = ["aab", "aaaa", "ababa", ""]
Output: [1, 2, 0, 0]

One replacement breaks the initial aa. Two alternating replacements separate four equal letters. The final two words already meet the condition.

Examples

Example 1
Input
(['aab', 'aaaa', 'ababa', ''],)
Output
[1, 2, 0, 0]
Notes
Even and odd-length runs contribute separately; an empty word contributes zero.
Example 2
Input
(['baabb', 'z'],)
Output
[2, 0]
Notes
Two separate equal pairs each require a replacement.

Constraints

  • •`0 <= len(words) <= 100000`. Total character count is at most 1000000.
  • •Each word contains only `a` through `z` and may be empty.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def minimum_replacements(words: list[str]) -> list[int]:
Sample call
minimum_replacements(['aab', 'aaaa', 'ababa', ''])
Language
Case 1
Input
(['aab', 'aaaa', 'ababa', ''],)
Expected
[1, 2, 0, 0]
Case 2
Input
(['baabb', 'z'],)
Expected
[2, 0]

Browse More Questions

Show the approach

Approach

A maximal run of k equal letters contains floor(k/2) disjoint adjacent equal pairs. Every valid result must change at least one character in each pair. Change the second character of every such pair. No two chosen positions inside a run are adjacent. Each can be changed to a lowercase letter different from its two neighbors, so the construction meets the lower bound. Between runs, the original boundary letters differ. If two changed positions are adjacent across a boundary, choose their replacement letters in order and avoid equality with both the already chosen left neighbor and the right neighbor if unchanged. The alphabet always has enough choices.

Count the needed changes without constructing the result. When two adjacent letters match, count one replacement and move past both characters. Otherwise advance one character. This partitions every run into disjoint pairs and preserves word order in the result.

Time complexity:
O(w + M) for w words and M total characters
Space complexity:
O(1) auxiliary space, plus the output