Problem · String
Minimum Replacements for Unequal Adjacent Characters
Learn this problemProblem statement
Given a lowercase string s, return the minimum number of replacement operations needed so that every pair of adjacent characters is different.
In one operation, choose one index and replace its character with any lowercase English letter. Insertions and deletions are not allowed.
Function
minimumReplacements(s: String) → intExamples
Example 1
s = "aab"return = 1Replace either of the first two a characters with a letter different from its neighbors.
Example 2
s = "aaaa"return = 2Two replacements can produce abab, and one replacement cannot break all three equal adjacencies.
Example 3
s = "abac"return = 0Every adjacent pair is already different.
Constraints
1 <= s.length <= 2 * 10^5.scontains only lowercase English letters.