Maximum Binary String After Change LeetCode solution in Python

1
# Runtime: 128 ms (Top 57.69%) | Memory: 15.4 MB (Top 69.23%)
2
class Solution:
3
def maximumBinaryString(self, binary: str) -> str:
4
zero = binary.count("0") # count number of '0'
5
zero_idx = (
6
binary.index("0") if zero > 0 else 0
7
) # find the index of fist '0' if exists
8
one = (
9
len(binary) - zero_idx - zero
10
) # count number of '1' (not including leading '1's)
11
return f"{binary[:zero_idx]}{'1'*(zero-1)}{'0'*min(zero, 1)}{'1'*one}"

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0