👏 Beats 100.00% ✅ Java ✅ C++ ✅ Python ✅ O(N) ✅
Intuition\n###### The problem involves grouping black and white balls on a table by swapping adjacent balls. The key insight is to iterate through the string fr
# Intuition\n###### The problem involves grouping black and white balls on a table by swapping adjacent balls. The key insight is to iterate through the string from right to left, keeping track of the count of white balls encountered. This way, we can efficiently calculate the minimum steps required to separate black and white balls.\n\n# Approach\n###### The approach involves traversing the input string from right to left. For each black ball (\'1\') encountered, the algorithm adds the current count of white balls to the result. If a white ball (\'0\') is found, it increments the count of white balls. This process continues until the entire string is traversed.\n\n---\n\n# Let\'s understand this by using Example:\n### Example: "10110"\n### Step 1:\n###### The algorithm starts iterating from the right end of the string:\n- At position 5 (index 4), there\'s \'0\', indicating a white ball. Increment the count of white balls (cnt = 1).\n\n### Step 2:\n###### Move to the next position:\n- At position 4 (index 3), there\'s \'1\', indicating a black ball. Add the current count of white balls (cnt) to the result (ans = 1). The count of white balls remains the same (cnt = 1).\n\n### Step 3:\n###### Move to the next position:\n- At position 3 (index 2), there\'s \'1\', indicating a black ball. Add the current count of white balls (cnt) to the result (ans = 1 + 1 = 2). The count of white balls remains the same (cnt = 1).\n\n### Step 4:\n###### Move to the next position:\n- At position 2 (index 1), there\'s \'0\', indicating a white ball. Increment the count of white balls (cnt = 2).\n\n### Step 5:\n###### Move to the next position:\n- At position 1 (index 0), there\'s \'1\', indicating a black ball. Add the current count of white balls (cnt) to the result (ans = 2 + 2 = 4). The count of white balls remains the same (cnt = 2).\n\n# Result\n###### The minimum number of steps to group all the black balls to the right and all the white balls to the left is 4.\n\n###### This example demonstrates how the algorithm efficiently calculates the minimum steps needed to separate black and white balls by swapping adjacent balls.\n\n---\n\n# Complexity\n- ###### Time complexity: \n\n    - The algorithm has a time complexity of $$O(n)$$, where $$n$$ is the length of the input string.\n\n- ###### Space complexity:\n\n    - The algorithm has a space complexity of $$O(1)$$, as it uses a constant amount of space for variables.\n\n---\n\n# Code\n### Java:\n```\nclass Solution {\n    public long minimumSteps(String s) {\n        long cnt = 0, ans = 0;\n        for(int i = s.length() - 1; i >= 0; i--) {\n            if(s.charAt(i) == \'1\') {\n                ans += cnt;\n            } else {\n                cnt++;\n            }\n        }\n        return ans;\n    }\n}\n```\n\n### C++:\n```\nclass Solution {\npublic:\n    long long minimumSteps(string s) {\n        long long cnt = 0, ans = 0;\n        for (int i = s.length() - 1; i >= 0; i--) {\n            if (s[i] == \'1\') {\n                ans += cnt;\n            } else {\n                cnt++;\n            }\n        }\n        return ans;\n    }\n};\n```\n\n### Python:\n```\nclass Solution:\n    def minimumSteps(self, s):\n        cnt = 0\n        ans = 0\n        for i in range(len(s) - 1, -1, -1):\n            if s[i] == \'1\':\n                ans += cnt\n            else:\n                cnt += 1\n        return ans\n```\n\n# Screenshot\n![Screenshot 2024-10-15 222707.png](https://assets.leetcode.com/users/images/c1847bf0-9b9a-43cd-ba5d-870454706462_1729013325.9305346.png)
['String', 'Python', 'C++', 'Java', 'Python3']