You are given a string s of lowercase English letters. You can reprogram any character of s by replacing it with any other lowercase English letter. Your goal is to make the string alternating, meaning no two adjacent characters are equal.
Return the minimum number of character replacements needed to make s alternating.
Function Signature
def min_reprogramming(s: str) -> int:
Rules
-
Each replacement changes the character at one position to a different lowercase letter. The new letter may already appear elsewhere in
s
.
-
The answer is the number of positions changed. A string of length 1 is already alternating.
Constraints
-
1 <= len(s) <= 10^5
-
s
consists of lowercase English letters only.
Examples
Input: s = "aabbcc"
Output: 3
Replacing the second character of each equal pair works, for example "aabbcc" becomes "axbxcx". Each of the three pairs "aa", "bb" and "cc" needs at least one change, so 3 is the minimum.
Input: s = "abcde"
Output: 0
The string is already alternating, so no replacements are needed.
Input: s = "aaaaa"
Output: 2
Changing positions 1 and 3 gives a string such as "abaca". One change is not enough, because the pairs at positions 0-1 and 2-3 each need their own change.