diff options
Diffstat (limited to 'max_odd_binary_num.py')
-rw-r--r-- | max_odd_binary_num.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/max_odd_binary_num.py b/max_odd_binary_num.py new file mode 100644 index 0000000..b8235ef --- /dev/null +++ b/max_odd_binary_num.py @@ -0,0 +1,29 @@ +# 2864. Maximum Odd Binary Number + +""" +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. +""" + + +class Solution(object): + def maximumOddBinaryNumber(self, s): + """ + :type s: str + :rtype: str + """ + c = sorted(s) + for i in range(len(c) - 1, -1, -1): + if c[i] == "1": + c[i], c[-1] = c[-1], c[i] + break + return "".join(c) + + +if __name__ == "__main__": + obj = Solution() + print(obj.maximumOddBinaryNumber("010")) + print(obj.maximumOddBinaryNumber("0101")) |