1702. Maximum Binary String After Change
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 47% Topics: String, Greedy
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
class Solution(object):
def maximumBinaryString(self, binary):
"""
:type binary: str
:rtype: str
"""
result = list(binary)
zeros = ones = 0
for i, c in enumerate(result):
if c == '0':
zeros += 1
elif zeros == 0:
ones += 1
result[i] = '1'
if ones != len(result):
result[zeros+ones-1] = '0'
return "".join(result)
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions