Minimum Replacements to Eliminate Consecutive Equal Triples
StringsAuto-graded · 24 test casesLast updated
Find the total minimum number of character modifications required to prevent any character from repeating 3 or more times consecutively across a collection of strings.
Given a list of strings made of lowercase English letters, your goal is to ensure that no string contains three or more identical characters in a row (for instance, "aa" is allowed, but "aaa" is forbidden).
You can substitute any character in any string with another lowercase letter. Calculate the overall minimum number of character replacements needed across all strings in the list to satisfy this constraint.
Constraints
- The list contains up to $10^4$ strings.
- The total length of all strings combined does not exceed $10^6$.
- Every string consists entirely of lowercase English letters (
'a'to'z').
Edge Cases
- Strings with fewer than 3 characters require 0 replacements.
- Strings without any 3 consecutive identical characters require 0 replacements.
- Replacements in one string do not affect any other string.
Examples
Example 1
Input: min_replacements_to_separate(['aaabbb'])
Output: 2
Example 2
Input: min_replacements_to_separate(['aaaaa'])
Output: 1
Hints
Hint 1
Break each string down into runs of consecutive identical characters.
Hint 2
For a single continuous streak of identical letters of length $L$, how many replacements are strictly required?
Hint 3
Replacing every 3rd character in a streak prevents any 3 consecutive matching characters without combining adjacent streaks.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem can be solved greedily by analyzing continuous sequences (or "runs") of identical characters independently.
For any contiguous sequence of $L$ identical characters, we can divide it into non-overlapping blocks of 3 characters. Inside each block of 3, at least 1 character must be modified to prevent three identical letters in a row. Therefore, the minimum number of replacements needed for a run of length $L$ is:
$$\lfloor L / 3 \rfloor$$
Since there are 26 lowercase English letters available, we can always select a substitute character that differs from both its left and right neighbors. This guarantees that changing a character inside a run will never accidentally create a new triple with adjacent characters or bridge two separate runs. Thus, each run can be processed independently.
Algorithm
Instead of explicitly finding run lengths or modifying strings, we can scan each string character by character:
- Maintain a count of consecutive matching characters (
run_length). - If the current character matches the previous one, increment
run_length. - When
run_lengthreaches 3, record 1 replacement and resetrun_lengthto 0. Resetting to 0 simulates substituting the 3rd character with a unique letter, effectively breaking the current streak. - If the current character differs from the previous one, reset
run_lengthto 1.
Summing these operations across all words yields the minimum replacements required.
Complexity
- Time Complexity: $\mathcal{O}(N)$, where $N$ is the total number of characters across all words in the input. Each character is visited once.
- Space Complexity: $\mathcal{O}(1)$ auxiliary space, as only integer counters are maintained.
from typing import List
def min_replacements_to_separate(words: List[str]) -> int:
total_replacements = 0
for word in words:
run_length = 1
for i in range(1, len(word)):
if word[i] == word[i - 1]:
run_length += 1
if run_length == 3:
total_replacements += 1
run_length = 0
else:
run_length = 1
return total_replacements