GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #2864 - Maximum Odd Binary Number - LeetCode JavaScript Solutions

Back to all solutions

#2864 - Maximum Odd Binary Number

Easy • View on LeetCode • View on GitHub
String Math Greedy

Problem Description

You are given a binary string s that contains at least one '1'.

You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination.

Return a string representing the maximum odd binary number that can be created from the given combination.

Note that the resulting string can have leading zeros.

Solution

/**
 * @param {string} s
 * @return {string}
 */
var maximumOddBinaryNumber = function(s) {
  let ones = 0;
  for (const bit of s) {
    if (bit === '1') ones++;
  }
  return '1'.padStart(ones, '1').padEnd(s.length, '0').slice(1) + '1';
};
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.