Beginner Friendly | BEATS 100% | GREEDY | SIMPLE | STEP BY STEP | JAVA GOATED
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nWe want to count the number of swaps we have to make to put all the 1s on the rig
![Screenshot 2024-10-15 155804.png](https://assets.leetcode.com/users/images/5db064ef-97b7-4751-b4e9-5da4265b8020_1728979809.162658.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe want to count the number of swaps we have to make to put all the 1s on the right, and 0s on the left.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nRealize that when we look at the string `101` from the back, the first `1` from the right doesnt need to be swapped, because there aren\'t any zeros to its right. When we reach the second `1`, we have a `0` in between and it results in a swap needed.\n\nWhat this means is that for each `1`, the number of swaps to shift it to the right is the number of `0`s on its right.\n\n\nWith that we can start coding and these are the steps:\n1. Initialize a `count` to keep track of our answer\n2. Keep track of the total number of `0`s encountered with `zeroNum`\n3. Loop through each `char c` in the string from right to left, starting from the character at index `s.length() - 1` and ending at index  `0`\n4. If `c ==\'0\'`, increment our number of `0`s encountered with `zeroNum++`\n5. Else, we found a `1` and we need to add the total number of `0`s encountered to our answer\n6. Once we are done with the string, `return count` and profit\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public long minimumSteps(String s) {\n        long count = 0;\n        long zeroNum = 0;\n        for(int i = s.length() - 1; i >= 0; i--){\n            char c = s.charAt(i);\n            if(c == \'0\'){\n                zeroNum++;\n            } else{\n                count += zeroNum;\n            }\n\n        }\n        return count;\n    }\n}\n```
['String', 'Greedy', 'Java']