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.