Skip to content

Commit 76a325f

Browse files
authored
Create 2864-maximum-odd-binary-number.js
1 parent e650410 commit 76a325f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Greedy
3+
* Time O(n) | Space O(n)
4+
* @param {string} s
5+
* @return {string}
6+
*/
7+
var maximumOddBinaryNumber = function(s) {
8+
9+
let numberOf1s = s.split("").filter((bit) => bit==="1").length;
10+
s = s.split("").map((bit) => "0");
11+
12+
let i = 0;
13+
while(numberOf1s > 1) {
14+
s[i] = "1";
15+
i++;
16+
numberOf1s--;
17+
}
18+
19+
s[s.length - 1] = 1;
20+
return s.join("");
21+
};

0 commit comments

Comments
 (0)