C++ O(n) greedy solution. with explanation.
\nLet\'s look at this example  \n111100011100  \nWe don\'t need to touch the first part of the ones, they are already good, so we find the first 0 and start wit
\nLet\'s look at this example  \n111100011100  \nWe don\'t need to touch the first part of the ones, they are already good, so we find the first `0` and start with this index `prev`.   \naccoarding the rule "10" can be "01" then We can move all zeros after `prev`,   \n111100000111  \naccording the rule "00" can be "10", all the zeros can  be changed to `111..110` which is:  \n111111110111, we get the answer.  \n```\nclass Solution {\npublic:\n    string maximumBinaryString(string &s) {\n        int prev = 0;\n        while(prev < s.size() && s[prev] == \'1\') prev++;\n        int z = 0, o = 0;\n        for(int i = prev; i < s.size(); ++i) {\n            o += s[i] == \'1\';\n            z += s[i] == \'0\';\n        }\n        \n        for(int i = prev; i < s.size(); ++i) {\n            if(z > 1) {\n                s[i] = \'1\';\n                --z;\n            }else if(z == 1) {\n                s[i] = \'0\';\n                --z;\n            }else {\n                s[i] = \'1\';\n            }\n        }\n        return s;\n    }\n};\n```\n\nbecause there is only one `0` in the answer, we can calculate the 0\'s index and set this index to be `0`.\n```\nclass Solution {\npublic:\n    string maximumBinaryString(string &s) {\n        int prev = 0, zeros = 0;\n        while(prev < s.size() && s[prev] == \'1\') prev++;\n        if(prev == s.size()) return s;\n        for(int i = prev; i < s.size(); ++i) {\n            zeros += s[i] == \'0\';\n        }\n        string ans(s.size(), \'1\');\n        ans[prev + zeros - 1] = \'0\';\n        return ans;\n    }\n};\n```\n\n
[]