KatInterview

Minimum Replacements to Eliminate Consecutive Equal Triples

StringsAuto-graded · 24 test cases
EasyAuto-gradedStringsstringsgreedy

Last 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.