1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public String maximumOddBinaryNumber(String s) { int oneCount = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '1') { oneCount++; } } int zeroCount = s.length() - oneCount; StringBuilder sb = new StringBuilder(); while (oneCount > 1) { sb.append("1"); oneCount--; } while (zeroCount > 0) { sb.append("0"); zeroCount--; } sb.append("1"); return sb.toString(); } }
|
References
2864. Maximum Odd Binary Number