Minimum Character Replacements So No Two Adjacent Characters Are Equal
Company: Headlands
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Given a string of lowercase letters, find the minimum number of single-position replacements needed so that no two adjacent characters are equal. It tests turning an adjacency constraint into an exact minimum count, justifying why fewer operations cannot work, and handling edge cases in an efficient solution.
Read the full Headlands Data Scientist interview experience this question came from
Constraints
- 1 <= len(s) <= 100000
- Every character of s is a lowercase English letter from 'a' to 'z'.
Examples
Input: ('a',)
Expected Output: 0
Explanation: A single character is always valid.
Input: ('zz',)
Expected Output: 1
Explanation: Two equal letters at the alphabet edge need one change.
Hints
- Think about which parts of the string actually need to change: look at stretches of consecutive identical characters.
- Can a change inside one stretch of identical characters ever affect a different stretch?
- For a single stretch of identical characters of length L, find a lower bound on the changes needed and check whether it can be achieved.