Given a binary string s
(i.e., a string consisting only of the characters '0' and '1'), your task is to determine the minimum number of character changes required to make s
alternate between '0' and '1'. An alternating binary string is one where no two adjacent characters are the same. For example, "0101" and "1010" are alternating, but "0011" and "110" are not.
s
to either '0' or '1'.s
alternating.Constraints:
1 <= s.length <= 10^4
s
consists only of '0' and '1'.The problem asks for the minimum number of changes to turn a binary string into an alternating one. At first glance, you might consider checking every possible way to alternate the string by flipping characters one by one, but that would be inefficient.
Instead, notice that an alternating string can only be of two forms:
We want to efficiently determine the minimum number of changes required. Here’s how we approach it:
i
, determine what it should be for both patterns.This approach is efficient because it only requires a single pass through the string and constant extra space.
Let's walk through the example s = "0100"
:
Changes needed: 1
Changes needed: 3
The minimum number of changes required is 1.
class Solution:
def minOperations(self, s: str) -> int:
changes_start_0 = 0
changes_start_1 = 0
for i, c in enumerate(s):
expected_0 = '0' if i % 2 == 0 else '1'
expected_1 = '1' if i % 2 == 0 else '0'
if c != expected_0:
changes_start_0 += 1
if c != expected_1:
changes_start_1 += 1
return min(changes_start_0, changes_start_1)
class Solution {
public:
int minOperations(string s) {
int changes_start_0 = 0, changes_start_1 = 0;
for (int i = 0; i < s.size(); ++i) {
char expected_0 = (i % 2 == 0) ? '0' : '1';
char expected_1 = (i % 2 == 0) ? '1' : '0';
if (s[i] != expected_0) ++changes_start_0;
if (s[i] != expected_1) ++changes_start_1;
}
return min(changes_start_0, changes_start_1);
}
};
class Solution {
public int minOperations(String s) {
int changesStart0 = 0, changesStart1 = 0;
for (int i = 0; i < s.length(); i++) {
char expected0 = (i % 2 == 0) ? '0' : '1';
char expected1 = (i % 2 == 0) ? '1' : '0';
if (s.charAt(i) != expected0) changesStart0++;
if (s.charAt(i) != expected1) changesStart1++;
}
return Math.min(changesStart0, changesStart1);
}
}
var minOperations = function(s) {
let changesStart0 = 0, changesStart1 = 0;
for (let i = 0; i < s.length; i++) {
let expected0 = i % 2 === 0 ? '0' : '1';
let expected1 = i % 2 === 0 ? '1' : '0';
if (s[i] !== expected0) changesStart0++;
if (s[i] !== expected1) changesStart1++;
}
return Math.min(changesStart0, changesStart1);
};
Brute-force approach: If you tried all possible combinations or performed swaps, you might have exponential time complexity, which is impractical for large strings.
Optimized approach: The solution above performs a single pass through the string, comparing each character to the two possible alternating patterns.
s
, because we check each character once.To solve the Minimum Changes To Make Alternating Binary String problem, we recognize that only two alternating patterns are possible. By counting the mismatches for both patterns in a single pass, we efficiently determine the minimum number of changes needed. This approach is simple, elegant, and optimal, making it a great example of reducing a problem to its fundamental patterns and leveraging symmetry for efficiency.