aboutsummaryrefslogtreecommitdiff
path: root/max_odd_binary_num.py
blob: b8235ef36fb5b01895cad590f72ec49a25446818 (plain)
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
# 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"))