Tap here to switch tabs
Problem
Submissions

Problem: Separate Black and White Balls

Separate Black and White Balls asks you to use adjacent swaps on a binary string to group every white ball on the left and every black ball on the right, using as few swaps as possible.

Statement

You are given a $0$ indexed binary string s of length n, where '$1$' represents a black ball and '$0$' represents a white ball.

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

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

Constraints:

  • 1≤1 \leq n == s.length ≤105\leq 10^5

  • s[i] is either '$0$' or '$1$'.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Separate Black and White Balls

Separate Black and White Balls asks you to use adjacent swaps on a binary string to group every white ball on the left and every black ball on the right, using as few swaps as possible.

Statement

You are given a $0$ indexed binary string s of length n, where '$1$' represents a black ball and '$0$' represents a white ball.

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

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

Constraints:

  • 1≤1 \leq n == s.length ≤105\leq 10^5

  • s[i] is either '$0$' or '$1$'.