maximum-odd-binary-number.py (792B)
1 # Not great solution 2 3 # 5.93% 93ms 4 # 75.75% 16.56MB 5 6 7 class Solution: 8 def maximumOddBinaryNumber(self, s: str) -> str: 9 ones = s.count("1") 10 lis = [] 11 for i in range(ones): 12 lis.append("1") 13 for i in range(len(s) - ones): 14 lis.append("0") 15 st = toStr(lis) 16 val = int(st, 2) 17 while val % 2 == 0: 18 newStr = moveRightMost(lis) 19 st = toStr(lis) 20 val = int(st, 2) 21 return st 22 23 24 def toStr(lis): 25 st = "" 26 for i in lis: 27 st += i 28 return st 29 30 31 def moveRightMost(s): 32 count = len(s) - 1 33 for i in reversed(s): 34 if i == "0" and s[count - 1] == "1": 35 s[count - 1] = "0" 36 s[count] = "1" 37 return s 38 count -= 1 39 return s