Counting Minimum Swaps to Move All '1's Before '0's ||O(n)
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem seems to be about counting how many steps are needed to make the string sat
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem seems to be about counting how many steps are needed to make the string satisfy a specific condition. Given that there are only two types of characters (\'1\' and \'0\'), the task could be about counting the swaps required to achieve a desired arrangement. The idea is that for every 1 that is found in the string before a 0, a swap must be performed to move the 1 to the left of the 0. Therefore, we can count the number of such 1s that exist to the left of each 0.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe traverse the string from left to right, maintaining a count of how many 1s have been encountered (onecount). For each 0 that is encountered, we increment the swap count by the current value of onecount, since every 1 before this 0 needs to be swapped to make the string valid. The process continues until the end of the string, and the total number of swaps is returned as the answer.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nhe solution involves a single traversal of the string, so the time complexity is O(n), where n is the length of the string s.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nhe space complexity is O(1), as we only use a few extra variables (onecount and swaps) independent of the input size.\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long minimumSteps(string s) {\n        long long n=s.size();\n        long long onecount=0;\n        long long swaps=0;\n        for(long long i=0;i<n;i++){\n            if(s[i]==\'1\') onecount++;\n            else swaps+=onecount;\n        }\n        return swaps;\n    }\n};\n```
['C++']