> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Separate Black and White Balls Python Solution

> Tested Python solution for LeetCode 2938 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 2938, [Medium](/catalog/medium). Topics: [Two Pointers](/catalog/topics/two-pointers), [String](/catalog/topics/string), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/separate-black-and-white-balls/description/).

Generate this problem as a practice environment: tested reference solution, 21 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2938   # by problem number
lcpy gen -s separate_black_and_white_balls   # by problem name
```

## Problem

There are `n` balls on a table, each ball has a color black or white.

You are given a 0-indexed binary string `s` of length `n`, where `1` and `0` represent black and white balls, respectively.

In each step, you can choose two adjacent balls and swap them.

Return *the minimum number of steps to group all the black balls to the right and all the white balls to the left*.

### Examples

```
Input: s = "101"
Output: 1
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "011".
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.
```

```
Input: s = "100"
Output: 2
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "010".
- Swap s[1] and s[2], s = "001".
It can be proven that the minimum number of steps needed is 2.
```

```
Input: s = "0111"
Output: 0
Explanation: All the black balls are already grouped to the right.
```

### Constraints

* 1 \<= n == s.length \<= 10^5
* s\[i] is either '0' or '1'.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/separate_black_and_white_balls/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/separate_black_and_white_balls/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def minimum_steps(self, s: str) -> int:
        steps = 0
        ones = 0
        for ball in s:
            if ball == "1":
                ones += 1
            else:
                steps += ones
        return steps
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.