FastPrepMinimum Changes for Distinct Adjacent Characters
Problems/Minimum Changes for Distinct Adjacent Characters

Minimum Changes for Distinct Adjacent Characters

Headlands Technologies logoHeadlands Technologies● EasyFULLTIMEOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The visible report directly specifies a string, replacement of any one position by an arbitrary character, and minimizing the number of replacements until adjacent characters differ. The callable interface, lowercase alphabet, bounds, and examples are authored only to make the reported task executable; they do not change its requested result.

You are given a string s. In one operation, you may choose any position and replace its character with any lowercase English letter.

Return the minimum number of operations needed so that no two adjacent characters in s are equal.

Function

minimumChangesForDistinctAdjacentCharacters(s: String) → int

Examples

Example 1

s = "aaab"return = 1

Change the middle 'a' to 'b', producing "abab".

Example 2

s = "aaaaa"return = 2

Two replacements are sufficient, for example to obtain "ababa", and one replacement cannot break all four equal adjacencies.

Example 3

s = "abcde"return = 0

Every adjacent pair already contains different characters.

Constraints

  • 1 <= s.length <= 200000.
  • s contains only lowercase English letters.

More Headlands Technologies problems

  • Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
  • Array Interval SumsOA · Seen Oct 2026
  • Job Runner Chain ReportsOA · Seen Oct 2026
All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public int minimumChangesForDistinctAdjacentCharacters(String s) {
        // write your code here
    }
}
Checking account…