simple solution with explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nThis code defines a method to calculate the minimum number of steps (or "swaps") requir
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis code defines a method to calculate the minimum number of steps (or "swaps") required to rearrange characters in a string s such that all \'1\'s (representing black pieces) appear before all \'0\'s (representing white pieces) in a single pass.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nMy approach for this problem is actually very simple.\n```\nfor (int i = 0; i < s.length(); i++) {\n    if (s.charAt(i) == \'0\') \n        swap += (long) black; \n    else\n        black++; \n}\n\n```\n\nFor each character in the string:\nIf the character is \'0\', this means that any \'1\'s seen before this \'0\' need to be moved to the left of this \'0\'. So, it adds the current count of black to swap (because all \'1\'s before this \'0\' need to be swapped past this \'0\').\nIf the character is \'1\', it simply increments the black counter, as this helps track the number of \'1\'s that need to be moved when a \'0\' is encountered.\n\n```\nreturn swap;\n\n```\n\nAfter processing the entire string, swap will contain the minimum number of swaps required to arrange all \'1\'s before all \'0\'s, so the code returns swap.\nExample Walkthrough\nFor s = "0101":\n\nAt i = 0, s.charAt(0) == \'0\', no blacks so swap = 0.\nAt i = 1, s.charAt(1) == \'1\', increment black to 1.\nAt i = 2, s.charAt(2) == \'0\', add black (which is 1) to swap, so swap = 1.\nAt i = 3, s.charAt(3) == \'1\', increment black to 2.\nFinal swap = 1, which is the minimum steps required to rearrange "0101" to "1100".\n\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def minimumSteps(self, s: str) -> int:\n        swap = 0\n        black = 0\n        \n        for char in s:\n            if char == \'0\':\n                swap += black\n            else:\n                black += 1\n                \n        return swap\n\n```\n```java []\nclass Solution {\n    public long minimumSteps(String s) {\n        long swap = 0;\n        int black = 0; \n        for (int i = 0; i < s.length(); i++) {\n            if (s.charAt(i) == \'0\') \n                swap += (long) black; \n            else\n                black++; \n        }\n        return swap;\n    }\n}\n```\n```cpp []\nclass Solution {\npublic:\n    long long minimumSteps(string s) {\n        long long swap = 0;\n        int black = 0;\n        \n        for (int i = 0; i < s.length(); i++) {\n            if (s[i] == \'0\') \n                swap += static_cast<long long>(black);\n            else\n                black++;\n        }\n        \n        return swap;\n    }\n};\n```\n![d971a77c-9c71-4405-86b6-2727fd435122_1720044713.1503446.jpeg](https://assets.leetcode.com/users/images/9ad02596-ba0b-4eec-9f89-9ff6733f29ee_1728967379.2533543.jpeg)\n
['C++', 'Java', 'Python3']