Maximum Binary String After Change LeetCode solution in Python
1# Runtime: 128 ms (Top 57.69%) | Memory: 15.4 MB (Top 69.23%)2class Solution:3def maximumBinaryString(self, binary: str) -> str:4zero = binary.count("0") # count number of '0'5zero_idx = (6binary.index("0") if zero > 0 else 07) # find the index of fist '0' if exists8one = (9len(binary) - zero_idx - zero10) # count number of '1' (not including leading '1's)11return f"{binary[:zero_idx]}{'1'*(zero-1)}{'0'*min(zero, 1)}{'1'*one}"