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 = 0 and expected character cur = 0 (pattern starting with '0').
For each character in the string:
If the character does not match cur, increment cnt1.
Toggle cur using XOR with 1.
Repeat with cur = 1 (pattern starting with '1') to get cnt2.
classSolution:defminOperations(self, s:str)->int:
cur = cnt1 =0for c in s:ifint(c)!= cur:
cnt1 +=1
cur ^=1
cur =1
cnt2 =0for c in s:ifint(c)!= cur:
cnt2 +=1
cur ^=1returnmin(cnt1, cnt2)
publicclassSolution{publicintminOperations(String s){int cur =0, cnt1 =0;for(char c : s.toCharArray()){if(c -'0'!= cur){
cnt1++;}
cur ^=1;}
cur =1;int cnt2 =0;for(char c : s.toCharArray()){if(c -'0'!= cur){
cnt2++;}
cur ^=1;}returnMath.min(cnt1, cnt2);}}
classSolution{public:intminOperations(string s){int cur =0, cnt1 =0;for(char c : s){if(c -'0'!= cur){
cnt1++;}
cur ^=1;}
cur =1;int cnt2 =0;for(char c : s){if(c -'0'!= cur){
cnt2++;}
cur ^=1;}returnmin(cnt1, cnt2);}};
classSolution{/**
* @param {string} s
* @return {number}
*/minOperations(s){let cur =0,
cnt1 =0;for(let c of s){if(parseInt(c)!== cur){
cnt1++;}
cur ^=1;}
cur =1;let cnt2 =0;for(let c of s){if(parseInt(c)!== cur){
cnt2++;}
cur ^=1;}return Math.min(cnt1, cnt2);}}
publicclassSolution{publicintMinOperations(string s){int cur =0, cnt1 =0;foreach(char c in s){if(c -'0'!= cur){
cnt1++;}
cur ^=1;}
cur =1;int cnt2 =0;foreach(char c in s){if(c -'0'!= cur){
cnt2++;}
cur ^=1;}return Math.Min(cnt1, cnt2);}}
funcminOperations(s string)int{
cur, cnt1 :=0,0for_, c :=range s {ifint(c-'0')!= cur {
cnt1++}
cur ^=1}
cur =1
cnt2 :=0for_, c :=range s {ifint(c-'0')!= cur {
cnt2++}
cur ^=1}if cnt1 < cnt2 {return cnt1
}return cnt2
}
class Solution {funminOperations(s: String): Int {var cur =0var cnt1 =0for(c in s){if(c -'0'!= cur){
cnt1++}
cur = cur xor1}
cur =1var cnt2 =0for(c in s){if(c -'0'!= cur){
cnt2++}
cur = cur xor1}returnminOf(cnt1, cnt2)}}
classSolution{funcminOperations(_ s:String)->Int{var cur =0var cnt1 =0for c in s {ifInt(String(c))!!= cur {
cnt1 +=1}
cur ^=1}
cur =1var cnt2 =0for c in s {ifInt(String(c))!!= cur {
cnt2 +=1}
cur ^=1}returnmin(cnt1, cnt2)}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1)
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.
class Solution {funminOperations(s: String): Int {var count =0for(i in s.indices){if(i %2==0){if(s[i]=='0'){
count++}}else{if(s[i]=='1'){
count++}}}returnminOf(count, s.length - count)}}
classSolution{funcminOperations(_ s:String)->Int{var count =0let chars =Array(s)for i in0..<chars.count {if i %2==0{if chars[i]=="0"{
count +=1}}else{if chars[i]=="1"{
count +=1}}}returnmin(count, chars.count - count)}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1)
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.