Minimum Number of Changes to Make Binary String Beautiful - Problem
You are given a 0-indexed binary string s having an even length.
A string is beautiful if it's possible to partition it into one or more substrings such that:
- Each substring has an even length
- Each substring contains only 1's or only 0's
You can change any character in s to 0 or 1.
Return the minimum number of changes required to make the string s beautiful.
Input & Output
Example 1 — Basic Case
$
Input:
s = "1001"
›
Output:
2
💡 Note:
We can partition into pairs: "10" and "01". First pair needs 1 change (10→00 or 10→11), second pair needs 1 change (01→00 or 01→11). Total: 2 changes.
Example 2 — No Changes Needed
$
Input:
s = "10"
›
Output:
1
💡 Note:
Only one pair "10" where characters differ, so we need 1 change to make it "00" or "11".
Example 3 — Already Beautiful
$
Input:
s = "1100"
›
Output:
0
💡 Note:
We can partition as "11" and "00". Both substrings already contain identical characters, so 0 changes needed.
Constraints
- 2 ≤ s.length ≤ 105
- s has even length
- s consists only of '0' and '1'
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code