🔥Simplest Java code, single pass, O(n), faster than 100%, with explanation🔥
Intuition\nAt the end, the string will contain atmost one 0.\nWe want to find that one index at which this 0 will exist(if it should).\nCall that index lastIdx\
# Intuition\nAt the end, the string will contain atmost one 0.\nWe want to find that one index at which this 0 will exist(if it should).\nCall that index lastIdx\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nFirst of all we don\'t at all care about 1s.\nWe will always look for a pair of zeroes.\nWhen we reach the second zero of a pair, we manipulate the pair of zeroes, it results in only a single 0, whose index is 1 greater than the first 0 in the pair. \nBoth first and second 0s in the pair become 1.\nThus, similarly, we go on hunting for the next pair(actually second 0 of next pair, because first 0 is the one we just created!), and keep manipulating.\nThe index of the resultant 0 from each manipulation will be lastIdx.\nUltimetly, whatever value we finally have in lastIdx is the index where that one 0 in the ans string will exist.\n\n<!-- Describe your approach to solving the problem. -->\n\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\n# Code\n```\nclass Solution {\n    public String maximumBinaryString(String binary) {\n        int lastIdx = -1;\n        char[] num = binary.toCharArray();\n\n        for(int i = 0; i < num.length; i++) {\n            if(num[i] == \'0\') {\n                if(lastIdx == -1) lastIdx = i;\n                else lastIdx++;\n            }\n        }\n\n        StringBuilder ans = new StringBuilder();\n        ans.append("1".repeat(num.length));\n        if(lastIdx == -1) return ans.toString(); \n        ans.replace(lastIdx, lastIdx + 1, "0");\n        return ans.toString();\n    }\n}\n```\n\n# PLEASE UPVOTE IF YOU FOUND IT HELPFUL! (AND SMART ;) )\n\n
['Java']