Maximum Binary String After Change LeetCode solution in C++
1class Solution {2public:3string maximumBinaryString(string binary) {4int n = binary.size();5int z_count = 0; // count the number of zeros6int z_index = -1; // index of the leftmost zero that is why we start7// iterating from the right8for (int i = n - 1; i >= 0; i--) {9if (binary[i] == '0') {10z_count++;11z_index = i;12binary[i] = '1'; // changing all occurances to 113}14}15if (z_index != -1) // to check if there is atleast 1 zero16{17binary[z_index + z_count - 1] = '0'; // this is the only zero present18// -1 because the only zero is also included in zero count19}20return binary;21}22};