#41 2938. β«βͺ Separate Black and White Balls: Efficient Grouping with Minimal Swaps π§ π
In this problem, youβre tasked with grouping black β« and white βͺ balls represented as a binary string. The goal is to move all the black balls to the right side and all the white balls to the left side with the minimum number of adjacent swaps.
π Problem Statement
You are given a 0-indexed binary string s
of length n
, where:
'1'
(β«) represents a black ball.'0'
(βͺ) represents a white ball.
In each step, you can choose two adjacent balls and swap them. The objective is to group all the black balls (β«) to the right and all the white balls (βͺ) to the left with the minimum number of swaps.
Constraints:
1 <= n == s.length <= 10^5
s[i]
is either'0'
or'1'
.
π Example:
Example 1:
Input: s = "101"
Output: 1
Explanation: You can swap s[0]
with s[1]
to get "011"
. It requires 1 step.
Example 2:
Input: s = "100"
Output: 2
Explanation:
- Swap the first and second balls (
"100"
->"010"
). - Swap the second and third balls (
"010"
->"001"
).
Example 3:
Input: s = "0111"
Output: 0
Explanation: The black balls are already grouped together on the right.
π Approach & Intuition
The goal is to group all the black balls to the right while minimizing the number of adjacent swaps. A key insight is to count the number of white balls (βͺ) a black ball (β«) has to pass over in order to be grouped correctly.
Instead of moving balls explicitly, we use a counting approach:
- Traverse the string from right to left.
- Maintain a count of white balls (βͺ) encountered.
- Every time you encounter a black ball (β«), add the number of white balls (βͺ) encountered so far to the total number of swaps.
This method gives the exact number of swaps needed without actually performing the swaps.
π» Python Code:
class Solution:
def minimumSteps(self, s: str) -> int:
counter = 0 # To count white balls (0's)
steps = 0 # To count the minimum number of swaps
start = -1 # Start from the right end of the string
# Traverse the string from right to left
for _ in range(len(s)):
if s[start] == '0': # Encounter a white ball (βͺ)
counter += 1
else: # Encounter a black ball (β«)
steps += counter # Add the number of white balls passed over
start -= 1 # Move left
return steps
πΉοΈ Example Walkthrough
Letβs walk through the example s = "11000111"
step by step:
- Initial string:
s = "11000111"
- Start from the right end (
start = -1
) withcounter = 0
andsteps = 0
.
- Start from the right end (
- Step 1:
- Current character is
'1'
(β«). No swaps required, but this black ball will need to pass over white balls later. counter = 0
,steps = 0
.
- Current character is
- Step 2:
- Current character is
'1'
(β«). No swaps required yet. counter = 0
,steps = 0
.
- Current character is
- Step 3:
- Current character is
'1'
(β«). No swaps required yet. counter = 0
,steps = 0
.
- Current character is
- Step 4:
- Current character is
'0'
(βͺ). Increase the white ball counter. counter = 1
,steps = 0
.
- Current character is
- Step 5:
- Current character is
'0'
(βͺ). Increase the white ball counter. counter = 2
,steps = 0
.
- Current character is
- Step 6:
- Current character is
'0'
(βͺ). Increase the white ball counter. counter = 3
,steps = 0
.
- Current character is
- Step 7:
- Current character is
'1'
(β«). There are 3 white balls (βͺ) to pass over, so we addcounter = 3
tosteps
. counter = 3
,steps = 3
.
- Current character is
- Step 8:
- Current character is
'1'
(β«). There are still 3 white balls (βͺ) to pass over, so we addcounter = 3
tosteps
. - Final
counter = 3
,steps = 6
.
- Current character is
Final Result: The minimum number of swaps required to group all black balls to the right and all white balls to the left is 6
.
β±οΈ Time Complexity
- Time Complexity: The solution iterates through the string once, so the time complexity is O(n), where
n
is the length of the string. - Space Complexity: We use only a few extra variables (
counter
,steps
,start
), so the space complexity is O(1).
π Conclusion
This problem demonstrates how a counting-based approach can effectively minimize swaps by keeping track of how many white balls (βͺ) each black ball (β«) needs to pass. By processing the string from right to left, we ensure an efficient solution with a time complexity of O(n). π