One for loop! 🧠 with Dry run ✅💯
Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to group all the white balls (0s) to the left and all the black balls (1s) 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to group all the white balls (`0`s) to the left and all the black balls (`1`s) to the right by swapping adjacent balls. \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize a `counter` to keep track of the number of black balls (`1`s) encountered so far.\n2. Traverse the string `s`. For each white ball (`0`), add the `counter` (the number of black balls encountered so far) to the result because each of those black balls will need to "jump over" this white ball to reach the right side.\n3. For each black ball (`1`), increment the `counter` since this black ball might need to swap with white balls later on.\n4. Return the total result, which will be the minimum number of swaps needed.\n\n# Dry run\nexample `s = "010110"`:\n\n| Index | `s[i]` | Character | `counter` (Black balls so far) | Steps added to `result` | `result` (Total steps) |\n|-------|--------|------------|-------------------------------|-------------------------|------------------------|\n| 0     | 0      | White      | 0                             | 0                       | 0                      |\n| 1     | 1      | Black      | 1                             | 0                       | 0                      |\n| 2     | 0      | White      | 1                             | 1                       | 1                      |\n| 3     | 1      | Black      | 2                             | 0                       | 1                      |\n| 4     | 1      | Black      | 3                             | 0                       | 1                      |\n| 5     | 0      | White      | 3                             | 3                       | 4                      |\n### Step by step process\nHere\u2019s a dry run table for the example `s = "010110"`:\n\n| Index | `s[i]` | Character | `counter` (Black balls so far) | Steps added to `result` | `result` (Total steps) |\n|-------|--------|------------|-------------------------------|-------------------------|------------------------|\n| 0     | 0      | White      | 0                             | 0                       | 0                      |\n| 1     | 1      | Black      | 1                             | 0                       | 0                      |\n| 2     | 0      | White      | 1                             | 1                       | 1                      |\n| 3     | 1      | Black      | 2                             | 0                       | 1                      |\n| 4     | 1      | Black      | 3                             | 0                       | 1                      |\n| 5     | 0      | White      | 3                             | 3                       | 4                      |\n\n### Explanation:\n\n1. **Index 0 (White):** No black balls encountered yet, so no steps are added. `result` remains 0.\n2. **Index 1 (Black):** Encounter a black ball, increment `counter` to 1. No steps are added.\n3. **Index 2 (White):** There is 1 black ball before this white ball, so it takes 1 step to move the black ball past this white ball. Add `1` to `result`, making it 1.\n4. **Index 3 (Black):** Encounter another black ball, increment `counter` to 2. No steps are added.\n5. **Index 4 (Black):** Encounter another black ball, increment `counter` to 3. No steps are added.\n6. **Index 5 (White):** There are 3 black balls before this white ball, so it takes 3 steps to move them past this white ball. Add `3` to `result`, making it 4.\n\n### Final Result:\nThe minimum number of steps required is `4`.\n# Complexity\n- Time complexity: O(`n`), where `n` is length of the string `s`.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(`1`) not using any extra space.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public long minimumSteps(String s) {\n        long result = 0;\n\n        int counter = 0;\n        for(char ch : s.toCharArray())\n        {\n            if(ch == \'0\')\n            {\n                result += counter;\n            }\n            else\n            {\n                counter++;\n            }\n        }\n\n        return result;\n    }\n}\n```\n\n![image.png](https://assets.leetcode.com/users/images/924fb5e6-c0f4-4e61-bcdb-1a2701a62265_1728957193.7492182.png)\n
['Two Pointers', 'String', 'Greedy', 'Counting', 'Java']