Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    2938 - Separate Black and White Balls\.

    Medium

    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.

    Example 1:

    Input: s = "101"

    Output: 1

    Explanation: We can group all the black balls to the right in the following way:

    • Swap s0 and s1, s = "011".

    Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.

    Example 2:

    Input: s = "100"

    Output: 2

    Explanation: We can group all the black balls to the right in the following way:

    • Swap s0 and s1, s = "010".

    • Swap s1 and s2, s = "001".

    It can be proven that the minimum number of steps needed is 2.

    Example 3:

    Input: s = "0111"

    Output: 0

    Explanation: All the black balls are already grouped to the right.

    Constraints:

    • <code>1 <= n == s.length <= 10<sup>5</sup></code>

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

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Long minimumSteps(String s)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait