💡 C++ | O(n) | Simple Logic | With Full Explanation ✏️
Intuition\n Describe your first thoughts on how to solve this problem. \nThis problem involves rearranging a binary string such that all \'0\'s are positioned b
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis problem involves rearranging a binary string such that all \'0\'s are positioned before all \'1\'s, while minimizing the number of swaps. The first thought is to use a two-pointer approach where one pointer starts from the `left` (for \'1\'s) and the other from the `right` (for \'0\'s), and we swap any out-of-place characters. This allows us to efficiently count the necessary swaps.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Two Pointers Setup:**\n\n    - Use two pointers, `l` starting from the left (beginning of the string) and `r` starting from the right (end of the string).\n    - The left pointer `l` will look for \'1\'s in the wrong place (on the left side), and the right pointer `r` will look for \'0\'s in the wrong place (on the right side).\n2. **Swapping Logic:**\n\n    - If `s[r]` is \'1\', it\u2019s already in the correct position, so we simply move `r` to the left.\n    - If `s[l]` is \'0\', it\u2019s already in the correct position, so we move `l` to the right.\n    - If neither of these conditions is met, it means `s[l]` is \'1\' and `s[r]` is \'0\', which are out of order. So, we swap them and count the number of swaps as the distance between `l` and `r`. Then, we move both pointers inward (increment `l` and decrement `r`).\n3. **Result:**\n    - Keep track of the total number of swaps needed by adding the distance between the pointers every time a swap occurs.\n    - Continue the process until the left pointer crosses the right pointer. Finally, return the total number of swaps as the answer.\n\n# Complexity\n- Time complexity:\n```O(n)```\n\n- Space complexity:\n```O(1)```\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long minimumSteps(string s) {\n        long long ans = 0;  // to store the result (number of steps)\n        int l = 0;  // Left pointer \n        int r = s.size() - 1;  // Right pointer\n        while (l <= r) {\n            // If the right pointer points to \'1\', we move it one step to the left\n            if (s[r] == \'1\') {\n                r--;\n            }\n            // If the left pointer points to \'0\', we move it one step to the right\n            else if (s[l] == \'0\') {\n                l++;\n            }\n            // Otherwise, we need to swap the characters at the left and right pointers\n            else {\n                swap(s[l], s[r]);  // Swap \'0\' from the right with \'1\' from the left\n                ans += (r - l);  // Add the distance between the left and right pointers to the result\n                l++;  // Move the left pointer \n                r--;  // Move the right pointer \n            }\n        }\n        return ans;\n    }\n};\n\n```\n\n\n\n![anonupvote.jpeg](https://assets.leetcode.com/users/images/bf7ce48f-1a34-404c-8610-410d43112c2c_1720413559.1746094.jpeg)\n\n# Ask any doubt !!!\nReach out to me \uD83D\uDE0A\uD83D\uDC47\n\uD83D\uDD17 https://tushar-bhardwaj.vercel.app/
['Two Pointers', 'String', 'Greedy', 'C++']