-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum_binary_string_after_change.js
38 lines (35 loc) · 1.25 KB
/
maximum_binary_string_after_change.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// https://leetcode.com/problems/maximum-binary-string-after-change/description/
// 1702. Maximum Binary String After Change
// You can apply each of the following operations any number of times:
// Operation 1: If the number contains the substring "00", you can replace it with "10".
// For example, "00010" -> "10010"
// Operation 2: If the number contains the substring "10", you can replace it with "01".
// For example, "00010" -> "00001"
/**
* @param {string} binary
* @return {string}
*/
var maximumBinaryString = function(binary) {
let c_first_ones = 0,
c_zeros = 0,
l = binary.length;
while (c_first_ones<l && binary[c_first_ones]==='1') c_first_ones+=1
for (let i = 0; i < l; i++) {
if (binary[i]==='0') c_zeros+=1
}
// The solution formula
return '1'.repeat(c_first_ones)+
'1'.repeat(c_zeros>0 ? c_zeros-1 : 0)+
'0'.repeat(c_zeros>0 ? 1 : 0)+
'1'.repeat(l-c_first_ones-c_zeros)
};
console.log(maximumBinaryString('000110'));
// Output: "111011"
console.log(maximumBinaryString('01'));
// Output: "01"
console.log(maximumBinaryString('1101011'));
// Output: "1110111"
console.log(maximumBinaryString('0000'));
// Output: "1110"
console.log(maximumBinaryString('1111'));
// Output: "1111"