JAVA | Pattern | 99% |
\nclass Solution {\n    public String maximumBinaryString(String binary) {\n        //strategy: Pattern\n        //insight: there will always be a 0 if there ar
```\nclass Solution {\n    public String maximumBinaryString(String binary) {\n        //strategy: Pattern\n        //insight: there will always be a 0 if there are 0\'s to start with and the 0 will shift by the # of 0\'s - 1 to the right\n        int count = 0, index = -1;\n        //find the number of 0\'s in the string\n        //find the starting index for a 0\n        for (int i = binary.length() - 1; i >= 0; i--) {\n            count += (49 - binary.charAt(i));\n            index = (binary.charAt(i) == \'0\') ? i : index;\n        }\n        char[] data = new char[binary.length()];\n        Arrays.fill(data, \'1\');\n        //if there is a 0 it is shifted over count - 1 to the right\n        if (index != -1) {\n            data[index + count - 1] = \'0\';\n        }\n        return new String(data);\n    }\n}\n```
[]