2 POINTERS ARE ENOUGH ✅✅69_BEATS_100%✅✅🔥Easy Solution🔥With Explanation🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nwe need to find a black ball followed by a white ball so that we can swap them \ninturn
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe need to find a black ball followed by a white ball so that we can swap them \ninturn means we need to find a 0 followed by a 1 in the string \nwe can use 2 pointers to see how \n# Approach\n<!-- Describe your approach to solving the problem. -->\nwe will start at the ends of the string \nli=leftindex,ri=right index\nwe will check if the sequence is 1-0 we will swap them which means we will add the diff of their indices to the result \nif the seq is 1-1 we will move our ri inside ie ri-- to look for 0\'s\nif seq is 0-0 we will move the li inside ie li++ to look for 1\'s\nif seq is 0-1 we will move both the li,ri inside \n\nthats it finally we will return the result \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![wallhaven-3lv8j6.jpg](https://assets.leetcode.com/users/images/d63381e4-ad31-4f78-8888-5fdf2d61dfe6_1728954497.557783.jpeg)\n\n# Code\n```javascript []\nvar minimumSteps = function (s) {\n    let swaps = 0\n    let li = 0\n    let ri = s.length - 1\n    while (li < ri) {\n        if(s[li]==\'1\'){\n            if(s[ri]==\'0\'){\n                swaps+=ri-li\n                li++\n            }\n            ri--\n        }\n        else{\n            if(s[ri]==\'1\')ri--\n            li++\n        }\n    }\n    return swaps\n};\n```\n```python []\ndef minimum_steps(s):\n    swaps = 0\n    li = 0\n    ri = len(s) - 1\n    while li < ri:\n        if s[li] == \'1\':\n            if s[ri] == \'0\':\n                swaps += ri - li\n                li += 1\n            ri -= 1\n        else:\n            if s[ri] == \'1\':\n                ri -= 1\n            li += 1\n    return swaps\n\n```\n```java []\npublic class MinimumSteps {\n    public static int minimumSteps(String s) {\n        int swaps = 0;\n        int li = 0;\n        int ri = s.length() - 1;\n        while (li < ri) {\n            if (s.charAt(li) == \'1\') {\n                if (s.charAt(ri) == \'0\') {\n                    swaps += ri - li;\n                    li++;\n                }\n                ri--;\n            } else {\n                if (s.charAt(ri) == \'1\') ri--;\n                li++;\n            }\n        }\n        return swaps;\n    }\n}\n\n```\n```c []\n#include <stdio.h>\n#include <string.h>\n\nint minimumSteps(char* s) {\n    int swaps = 0;\n    int li = 0;\n    int ri = strlen(s) - 1;\n    while (li < ri) {\n        if (s[li] == \'1\') {\n            if (s[ri] == \'0\') {\n                swaps += ri - li;\n                li++;\n            }\n            ri--;\n        } else {\n            if (s[ri] == \'1\') ri--;\n            li++;\n        }\n    }\n    return swaps;\n}\n```\n```c++ []\n#include <iostream>\n#include <string>\n\nint minimumSteps(const std::string& s) {\n    int swaps = 0;\n    int li = 0;\n    int ri = s.length() - 1;\n    while (li < ri) {\n        if (s[li] == \'1\') {\n            if (s[ri] == \'0\') {\n                swaps += ri - li;\n                li++;\n            }\n            ri--;\n        } else {\n            if (s[ri] == \'1\') ri--;\n            li++;\n        }\n    }\n    return swaps;\n}\n```
['Two Pointers', 'String', 'Greedy', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']