🌟 Step-by-Step Guide to Grouping Black and White Balls! 💯🔥
\n\n## \uD83C\uDF1F Access Daily LeetCode Solutions Repo : click here\n\n---\n\n\n\n---\n\n# Intuition\nThe problem asks us to group all black balls (represente
![github_promotion_dark.png](https://assets.leetcode.com/users/images/bcb0fe0d-0cb4-4cb7-83bb-d038a318ee7f_1728951100.4371204.png)\n\n## **\uD83C\uDF1F Access Daily LeetCode Solutions Repo :** [click here](https://github.com/withaarzoo/LeetCode-Solutions)\n\n---\n\n![image.png](https://assets.leetcode.com/users/images/27a1cb69-a381-4a42-9b8a-f442efe14cb1_1728951033.5586247.png)\n\n---\n\n# Intuition\nThe problem asks us to group all black balls (represented by `1`s) to the right and all white balls (represented by `0`s) to the left. We are only allowed to swap adjacent balls. Our goal is to calculate the minimum number of swaps required to achieve this.\n\n### Key Insight:\nThe number of swaps needed is directly related to the number of white balls (`0`s) that each black ball (`1`) needs to "skip" over to reach its desired position. Each black ball needs to move past all white balls to its left, and for each white ball encountered, it requires one swap.\n\n---\n\n# Approach\n1. **Initialization**: We keep two variables:\n   - `cnt`: Tracks the cumulative number of black balls (`1`s) encountered so far.\n   - `ans`: Stores the total number of swaps needed.\n\n2. **Traversal**: Iterate through the string:\n   - If the current character is a white ball (`0`), it needs to be swapped with the black balls that are on its left. So, add the number of black balls encountered so far (`cnt`) to `ans`.\n   - If the current character is a black ball (`1`), simply increment the `cnt` to reflect that there\u2019s one more black ball that needs to be considered in future swaps.\n\n3. **Final Result**: The total swaps required to group all black balls to the right is stored in `ans`.\n\n---\n\n# Complexity\n- **Time complexity**: \n  The solution runs in \\(O(n)\\), where \\(n\\) is the length of the string, as we traverse the string only once.\n\n- **Space complexity**: \n  \\(O(1)\\), since we are only using a few extra variables regardless of the input size.\n\n---\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    long long minimumSteps(string s) {\n        int n = s.length();\n        long long ans = 0;\n        int cnt = 0; // Tracks the number of black balls (1s)\n\n        // Traverse through the string\n        for (int i = 0; i < n; i++) {\n            if (s[i] == \'0\') {\n                // White ball encountered, add the number of black balls on its left\n                ans += cnt;\n            } else {\n                // Black ball encountered, increment the black ball count\n                cnt++;\n            }\n        }\n        return ans;\n    }\n};\n\n```\n```Java []\nclass Solution {\n    public long minimumSteps(String s) {\n        long ans = 0;\n        int blackCount = 0; // Tracks the number of black balls (1s)\n\n        // Traverse through the string\n        for (int i = 0; i < s.length(); i++) {\n            if (s.charAt(i) == \'0\') {\n                // White ball encountered, add the number of black balls on its left\n                ans += blackCount;\n            } else {\n                // Black ball encountered, increment the black ball count\n                blackCount++;\n            }\n        }\n        return ans;\n    }\n}\n\n```\n```JavaScript []\n/**\n * @param {string} s\n * @return {number}\n */\nvar minimumSteps = function(s) {\n    let ans = 0;\n    let blackCount = 0; // Tracks the number of black balls (1s)\n\n    // Traverse through the string\n    for (let i = 0; i < s.length; i++) {\n        if (s[i] === \'0\') {\n            // White ball encountered, add the number of black balls on its left\n            ans += blackCount;\n        } else {\n            // Black ball encountered, increment the black ball count\n            blackCount++;\n        }\n    }\n\n    return ans;\n};\n\n```\n```Python []\nclass Solution:\n    def minimumSteps(self, s: str) -> int:\n        ans = 0\n        blackCount = 0 # Tracks the number of black balls (1s)\n\n        # Traverse through the string\n        for ch in s:\n            if ch == \'0\':\n                # White ball encountered, add the number of black balls on its left\n                ans += blackCount\n            else:\n                # Black ball encountered, increment the black ball count\n                blackCount += 1\n\n        return ans\n\n```\n```Go []\nfunc minimumSteps(s string) int64 {\n    var ans int64 = 0\n    var blackCount int = 0 // Tracks the number of black balls (1s)\n\n    // Traverse through the string\n    for i := 0; i < len(s); i++ {\n        if s[i] == \'0\' {\n            // White ball encountered, add the number of black balls on its left\n            ans += int64(blackCount)\n        } else {\n            // Black ball encountered, increment the black ball count\n            blackCount++\n        }\n    }\n\n    return ans\n}\n\n```\n\n---\n\n# Step-by-Step Detailed Explanation\n\n1. **Initialize Variables**: \n   - `cnt` or `blackCount`: This keeps track of how many black balls (`1`s) we\'ve encountered so far.\n   - `ans`: This variable stores the total number of swaps needed.\n\n2. **Iterate Over the String**:\n   - If we find a `0` (white ball), this ball needs to swap places with all the black balls encountered so far (`blackCount`). Therefore, we increment `ans` by `blackCount` (this represents how many swaps it takes to move this white ball past the black balls to its right).\n   - If we find a `1` (black ball), increment `blackCount` because now there\u2019s one more black ball on the left.\n\n3. **Return the Result**: \n   - After processing the entire string, `ans` contains the minimum number of swaps needed to group all black balls to the right and white balls to the left.\n\n---\n\n![upvote_cat.png](https://assets.leetcode.com/users/images/178b5275-5d54-4f46-bb2b-c548cdebfb8e_1728951255.8511252.png)\n
['Two Pointers', 'String', 'Greedy', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript']