C# One pass O(n) solution. Very effecient.
Intuition\n Describe your first thoughts on how to solve this problem. \nGiven the operations available in the problem statement, we can always group together z
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGiven the operations available in the problem statement, we can always group together zeros by "pushing" ones further down. The question at this stage becomes "When and where do we group the zeros".\n\nIf we can always group the zeros, then we can also always convert every zero except 1 into ones.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFind the full count of zeros, as well as the index of the leftMost zero.\n\nIf the count of zeros in the input string is less than or equal to 1, then we can just return the input string, because moving a zero up wouldn\'t allow for any conversion and it would just be pushing ones further down in the process.\n\nIf the count of zeros is greater than 1, that\'s where the fun begins! Well, not really, since the rest is very simple. Basically if the count is greater than 1 we can start constructing our new string using a StringBuilder object. First, use the append method to append \'1\' to our sb, with the second arguement being the number of times it appends. In this case we want to append \'1\' (first zero index + (number of zeros minus 1)) times. This might seem random, but if we reason it out:\n\nLets say first zero index is 2. If we append \'1\' 2 times then spots 0 and 1 will be filled with \'1\'. Meaning next index is index 2 - the space where the first zero is.\n\nThen we add (count of zeros - one) \'1\'s, which functions as converting every zero except one into a \'1\'.\n\nNext, we append our single \'0\'.\n\nand finally we fill the rest of the space difference between the input string length and our string builders length with \'1\'s, since we know we "used" all zeros in our previous operations.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n) - we go over the entire length of the input string one time while collecting our "zeros" data.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n) we use a SB object which will store an equivelent number of characters to the input string.\n\n# Code\n```\npublic class Solution {\n    public string MaximumBinaryString(string binary) {\n        int zeros = 0;\n        int firstZero = -1;\n        for (int i = 0; i < binary.Length; i++){\n            if (binary[i] == \'0\'){\n                zeros++;\n                if (firstZero == -1) firstZero = i;\n            }\n        }\n        if (zeros < 2) return binary;\n        StringBuilder sb = new();\n        sb.Append(\'1\', firstZero + (zeros - 1));\n        sb.Append("0");\n        sb.Append(\'1\', binary.Length - sb.Length);\n        return sb.ToString();\n    }\n}\n```
['C#']