Minimum Number of Changes to Make Binary String Beautiful (LeetCode 2914): Solution and Explanation
Given an even-length binary string s, return the fewest single-character changes that let s be cut into even-length blocks of one repeated character.
Intuition
An even-length block like 0000 or 111111 is just several 2-character blocks of the same bit placed side by side. So any beautiful string can be cut into length-2 blocks that are each 00 or 11.
That turns the problem into a local one: look at the aligned pairs s[0]s[1], s[2]s[3], and so on. The string is beautiful exactly when every one of these pairs is uniform.
Pairs do not overlap, so fixing one never disturbs another. A uniform pair costs nothing, and a mixed pair (01 or 10) costs exactly one flip. The answer is the number of mixed pairs.
Approach: Count mixed aligned pairs
- Set
changes = 0. - Walk
i = 0, 2, 4, ...up to the end of the string. - If
s[i] != s[i + 1], the pair is mixed: increasechanges. - Return
changes.
Step-by-step example
s = "1001"
- i = 0: the pair is "10". The bits differ, so one flip is needed. changes = 1.
- i = 2: the pair is "01". The bits differ again. changes = 2.
- Flipping s[1] and s[3] gives "1100", which splits into "11" and "00". The answer is 2.
Code: Python, Java, C++
1class Solution:
2 def minChanges(self, s: str) -> int:
3 changes = 0
4 for i in range(0, len(s), 2):
5 if s[i] != s[i + 1]:
6 changes += 1
7 return changes1class Solution {
2 public int minChanges(String s) {
3 int changes = 0;
4 for (int i = 0; i < s.length(); i += 2) {
5 if (s.charAt(i) != s.charAt(i + 1)) {
6 changes++;
7 }
8 }
9 return changes;
10 }
11}1class Solution {
2public:
3 int minChanges(string s) {
4 int changes = 0;
5 for (int i = 0; i < (int)s.size(); i += 2) {
6 if (s[i] != s[i + 1]) {
7 changes++;
8 }
9 }
10 return changes;
11 }
12};Time and space complexity
O(n)
Each pair is looked at once, and there are n / 2 pairs.
O(1)
Only the counter is stored.
Other approaches
Greedy run splitting (time O(n), space O(1))
Scan runs of equal characters and, whenever a run ends at an odd length, flip the next character to extend it. It reaches the same count but the bookkeeping is easy to get wrong.
Dynamic programming over positions (time O(n), space O(n))
Track the cost of ending at each index with a block of 0s or 1s of even length. Correct, but far more machinery than the pair argument needs.
Common mistakes
- Checking every neighbor pair (i, i + 1) for all i instead of only even i. Pairs that straddle a block boundary are allowed to differ.
- Trying to keep long runs together. Longer blocks never help, because any even block is already a chain of length-2 blocks.
- Counting both characters of a mixed pair. Only one of the two needs to change.
Frequently asked questions
Why is it enough to look at pairs that start at even indices?
Every beautiful string can be cut at every even index, since each even block splits into length-2 blocks. So the aligned pairs are the only boundaries that matter.
Which character of a mixed pair should be flipped?
Either one. The pair becomes 00 or 11, and both are valid blocks on their own.
What is the largest possible answer?
n / 2, when every aligned pair is mixed, for example 0101...01.