1758. Minimum Changes To Make Alternating Binary String - Explanation
Description
You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa.
The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not.
Return the minimum number of operations needed to make s alternating.
Example 1:
Input: s = "0100"
Output: 1Explanation: If you change the last character to '1', s will be "0101", which is alternating.
Example 2:
Input: s = "10"
Output: 0Explanation: s is already alternating.
Example 3:
Input: s = "1111"
Output: 2Explanation: You need two operations to reach "0101" or "1010".
Constraints:
1 <= s.length <= 10,000sconsists of characters'0'and'1'only.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- String Iteration - Comparing each character against expected patterns requires basic string traversal
- XOR Operation - Using XOR to toggle between 0 and 1 provides an elegant way to alternate expected characters
- Counting and Comparison - Understanding that mismatches for two complementary patterns sum to the string length enables optimization
1. Start with Zero and One
Intuition
An alternating binary string must follow one of two patterns: starting with '0' (like "010101...") or starting with '1' (like "101010..."). We simply count how many characters differ from each pattern and return the smaller count.
We use XOR to toggle the expected character at each position. Starting with 0, we XOR with 1 after each character to alternate between expecting 0 and 1.
Algorithm
- Initialize
cnt1 = 0and expected charactercur = 0(pattern starting with '0'). - For each character in the string:
- If the character does not match
cur, incrementcnt1. - Toggle
curusing XOR with1.
- If the character does not match
- Repeat with
cur = 1(pattern starting with '1') to getcnt2. - Return the
minofcnt1andcnt2.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Start with Zero or One
Intuition
We can optimize by counting mismatches for only one pattern. Notice that if a position mismatches the "start with 1" pattern, it must match the "start with 0" pattern, and vice versa. So the count for one pattern plus the count for the other equals the string length.
We count mismatches for the "start with 1" pattern (where even indices should be '1' and odd indices should be '0'). The count for the "start with 0" pattern is simply length - count.
Algorithm
- Initialize
count = 0. - For each index
i:- If
iis even ands[i] == '0', incrementcount. - If
iis odd ands[i] == '1', incrementcount.
- If
- Return the
minofcountandlength - count.
Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Only Checking One Pattern
An alternating binary string can start with either '0' (giving "010101...") or '1' (giving "101010..."). A common mistake is only counting mismatches against one pattern and returning that count. You must compare against both patterns and return the minimum of the two counts, or use the relationship that the two counts sum to the string length.
Incorrect Character-to-Integer Conversion
When comparing characters to expected values (0 or 1), ensure proper conversion. In many languages, '0' and '1' are characters with ASCII values 48 and 49, not the integers 0 and 1. Use c - '0' or parseInt(c) to convert correctly. Directly comparing the character '0' to the integer 0 will give wrong results in most languages.
Sign in to join the discussion