#1702
Maximum Binary String After Change
specialist · 910 · lc medium +31 · verified · 48% accepted · 528 likes · top 33%
Description
You are given a binary string binary of only '0's and '1's. You may apply either operation any number of times:
- Operation 1: Replace "00" with "10". For example, "00010" -> "10010".
- Operation 2: Replace "10" with "01". For example, "00010" -> "00001".
Return the maximum binary string obtainable. A binary string x is greater than y when x's decimal value exceeds y's.
Example 1:
Input: binary = "000110"
Output: "111011"
Explanation: A valid transformation sequence can be:
"000110" -> "000101"
"000101" -> "100101"
"100101" -> "110101"
"110101" -> "110011"
"110011" -> "111011"
Example 2:
Input: binary = "01"
Output: "01"
Explanation: "01" cannot be transformed any further.
Code
1
2
3