🔥TWO POINTERS🎯 |✨SUPER EASY BEGINNERS 👏
Intuition\nThe problem involves calculating the minimum number of steps (or swaps) needed to move all occurrences of \'0\' in the string to the beginning, prese
### **Intuition**\nThe problem involves calculating the minimum number of steps (or swaps) needed to move all occurrences of \'0\' in the string to the beginning, preserving the relative order of \'1\'s. Every time we encounter a \'0\', we count how many positions it needs to move left to reach its correct spot. This essentially translates to counting the distance each \'0\' is from its correct position.\n\n### **Approach**\n1. Traverse the string from left to right.\n2. Each time a \'0\' is encountered, calculate how far it is from its correct position. This distance is the difference between its current index and the number of \'0\'s that have already been placed correctly (`last`).\n3. Sum up all the swaps required to place each \'0\' at its correct position, and return the total number of swaps.\n\n### **Complexity**\n- **Time Complexity**: \\(O(n)\\), where \\(n\\) is the length of the string. We traverse the string once.\n- **Space Complexity**: \\(O(1)\\), since we are only using a few variables for tracking positions and the swap count.\n\n---\n\n### **Code Implementations**\n\n\n```dart []\nclass Solution {\n  int minimumSteps(String s) {\n    int n = s.length;\n    int last = 0;  // Tracks how many \'0\'s have been placed correctly\n    int swapCount = 0;\n    \n    for (int i = 0; i < n; i++) {\n      if (s[i] == "0") {\n        swapCount += (i - last);\n        last++;\n      }\n    }\n    return swapCount;\n  }\n}\n```\n\n\n```python []\nclass Solution:\n    def minimumSteps(self, s: str) -> int:\n        n = len(s)\n        last = 0  # Tracks how many \'0\'s have been placed correctly\n        swapCount = 0\n        \n        for i in range(n):\n            if s[i] == \'0\':\n                swapCount += (i - last)\n                last += 1\n        \n        return swapCount\n```\n\n\n```java []\nclass Solution {\n    public int minimumSteps(String s) {\n        int n = s.length();\n        int last = 0;  // Tracks how many \'0\'s have been placed correctly\n        int swapCount = 0;\n        \n        for (int i = 0; i < n; i++) {\n            if (s.charAt(i) == \'0\') {\n                swapCount += (i - last);\n                last++;\n            }\n        }\n        \n        return swapCount;\n    }\n}\n```\n\n\n```cpp []\nclass Solution {\npublic:\n    int minimumSteps(string s) {\n        int n = s.length();\n        int last = 0;  // Tracks how many \'0\'s have been placed correctly\n        int swapCount = 0;\n        \n        for (int i = 0; i < n; i++) {\n            if (s[i] == \'0\') {\n                swapCount += (i - last);\n                last++;\n            }\n        }\n        \n        return swapCount;\n    }\n};\n```\n\n\n```javascript []\nclass Solution {\n    minimumSteps(s) {\n        let n = s.length;\n        let last = 0;  // Tracks how many \'0\'s have been placed correctly\n        let swapCount = 0;\n        \n        for (let i = 0; i < n; i++) {\n            if (s[i] === \'0\') {\n                swapCount += (i - last);\n                last++;\n            }\n        }\n        \n        return swapCount;\n    }\n}\n```\n\n\n```go []\npackage main\n\nfunc minimumSteps(s string) int {\n    n := len(s)\n    last := 0   // Tracks how many \'0\'s have been placed correctly\n    swapCount := 0\n    \n    for i := 0; i < n; i++ {\n        if s[i] == \'0\' {\n            swapCount += (i - last)\n            last++\n        }\n    }\n    \n    return swapCount\n}\n```\n\n\n```c []\n#include <stdio.h>\n#include <string.h>\n\nint minimumSteps(const char* s) {\n    int n = strlen(s);\n    int last = 0;  // Tracks how many \'0\'s have been placed correctly\n    int swapCount = 0;\n    \n    for (int i = 0; i < n; i++) {\n        if (s[i] == \'0\') {\n            swapCount += (i - last);\n            last++;\n        }\n    }\n    \n    return swapCount;\n}\n\nint main() {\n    const char* s = "110100";\n    printf("Minimum steps: %d\\n", minimumSteps(s));\n    return 0;\n}\n```\n\n---\n![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style=\'width:250px\'}\n
['Two Pointers', 'C', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'Dart']