Medium
String
Updated Sep 2026

Minimum Number of Changes to Make Binary String Beautiful

Asked at JPMorgan

Problem

Minimum Number of Changes to Make Binary String Beautiful asks for the fewest bit flips so an even-length binary string can be split into substrings of even length that each contain only 0s or only 1s. It looks like a partitioning problem, but it collapses to checking fixed pairs.

Asked At

CompanyDifficulty
JPMorganMediumView all JPMorgan questions →

How to Think About It

1.

Any even-length block of equal characters can be cut into pairs of length 2. So a string is beautiful iff every aligned pair (s[0],s[1]), (s[2],s[3]), ... has equal characters.

2.

Key insight: pairs are independent. A pair with different characters needs exactly one flip; a matching pair needs none.

3.

So the answer is the number of indices i (even) where s[i] != s[i+1].

4.

Walkthrough for "1001": pairs 10 and 01 both mismatch -> 2 changes (for example to "1100").

5.

No DP is needed — the aligned-pair observation removes all interaction between choices.

Optimal Approach

Step 1: changes = 0.
Step 2: For i in 0, 2, 4, ...: if s[i] != s[i+1], changes += 1.
Step 3: Return changes.

Time: O(n). Space: O(1).

What Trips People Up in Real Interviews

1.

Setting up a DP over partition points. Explain why aligned pairs are enough, and the problem becomes trivial.

2.

Checking unaligned pairs like (s[1], s[2]). Only positions (2k, 2k+1) matter.

3.

Counting two flips per mismatched pair. One flip makes the pair equal.

4.

Trying to minimize the number of blocks — the question is only about flips.

Solution Code

def minChanges(s):
    return sum(1 for i in range(0, len(s), 2) if s[i] != s[i + 1])

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Minimum Number of Changes to Make Binary String Beautiful problem?

Minimum Number of Changes to Make Binary String Beautiful asks for the fewest bit flips so an even-length binary string can be split into substrings of even length that each contain only 0s or only 1s. It looks like a partitioning problem, but it collapses to checking fixed pairs.

How do you solve Minimum Number of Changes to Make Binary String Beautiful?

The optimal approach is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.

What companies ask Minimum Number of Changes to Make Binary String Beautiful?

Minimum Number of Changes to Make Binary String Beautiful is asked at JPMorgan. It is a medium difficulty problem.

What are common mistakes on Minimum Number of Changes to Make Binary String Beautiful?
  • Setting up a DP over partition points. Explain why aligned pairs are enough, and the problem becomes trivial.
  • Checking unaligned pairs like `(s[1], s[2])`. Only positions `(2k, 2k+1)` matter.
  • Counting two flips per mismatched pair. One flip makes the pair equal.
  • Trying to minimize the number of blocks — the question is only about flips.