Solution | JavaScript | C++ | Python
Intuition\n Describe your first thoughts on how to solve this problem. \n### 1. Count the Distance to the Nearest 0 from Index 0:\n   - Start from index 0.\n   
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n### 1. **Count the Distance to the Nearest `0` from Index `0`:**\n   - Start from index `0`.\n   - Find the nearest zero (`0`) in the array.\n   - The number of swaps required to bring this `0` to the front is equal to the distance from the current index (`i`) to the first index (`0`), which is `i`.\n   - Let\'s call this swap count `distance`.\n\n### 2. **Move the `0` to the Front and Reduce the Distance for the Next `0`:**\n   - After you "move" the first `0` to the front (imagine this has been done), the array effectively has one fewer element. Therefore, the next nearest `0` in the array will now have to move one fewer space.\n   - The distance to the next `0` would then be `i - 1`, because the first `0` is already in position.\n\nIn short, you repeatedly find the next nearest `0` and calculate how many swaps it would take to move that `0` to the correct position in front of the array, reducing the distance with each successive `0`.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n# Code\n```javascript []\n/**\n * @param {string} s\n * @return {number}\n */\nvar minimumSteps = function(s) {\n    let total_whites = 0, i = 0;\n    let res = 0;\n\n    // Loop through each character in the string `s`\n    while (i  < s.length) {\n        // If the current character is \'0\', it means we need to move this \'0\'\n        if (s[i] == \'0\') {\n            // Calculate how many swaps are needed to bring this \'0\' to the front\n            // `i - total_whites` gives the number of swaps for the current \'0\'\n            // Because `total_whites` tracks how many \'0\'s have already been moved\n            res += i - total_whites;\n            \n            // Increment total_whites as we\'ve moved another \'0\'\n            total_whites++;\n        }\n        // Move to the next character in the string\n        i++;\n    }\n    \n    // Return the total number of swaps (minimum steps)\n    return res;\n};\n```\n``` cpp []\nclass Solution {\npublic:\n    long long minimumSteps(string s) {\n        long long total_whites = 0, i = 0;\n        long long res = 0;\n\n        // Loop through the string `s`\n        while (i < s.size()) {\n            // Check if the current character is \'0\'\n            if (s[i] == \'0\') {\n                // Calculate swaps needed for the current \'0\'\n                // `i - total_whites` gives the number of swaps for this \'0\'\n                res += i - total_whites;\n                \n                // Increment the count of total \'0\'s moved\n                total_whites++;\n            }\n            // Move to the next character\n            i++;\n        }\n        \n        // Return the result (total minimum swaps)\n        return res;\n    }\n};\n```\n``` python []\nclass Solution:\n    def minimumSteps(self, s: str) -> int:\n        total_whites, i = 0, 0\n        res = 0\n\n        # Iterate through the string `s`\n        while i < len(s):\n            # If the current character is \'0\', we need to consider moving it\n            if s[i] == \'0\':\n                # The swap count is determined by the difference `i - total_whites`\n                # `total_whites` tracks how many \'0\'s have already been moved\n                res += i - total_whites\n                \n                # Increment the count of \'0\'s moved\n                total_whites += 1\n            # Move to the next character\n            i += 1\n        \n        # Return the total number of swaps (minimum steps)\n        return res\n```
['Two Pointers', 'String', 'Greedy', 'C++', 'Python3', 'JavaScript']