leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 4ba3bdeecd77fe3b1eb3353cb5107d9b94e982e4
parent 8bc31b4264fee36780ace2cf92fffb9cf4d18113
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Thu, 29 Feb 2024 22:09:53 -0600

Completed maximum odd binary number

Diffstat:
Amaximum-odd-binary-number/maximum-odd-binary-number.py | 36++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+), 0 deletions(-)

diff --git a/maximum-odd-binary-number/maximum-odd-binary-number.py b/maximum-odd-binary-number/maximum-odd-binary-number.py @@ -0,0 +1,36 @@ +#Not great solution + +#5.93% 93ms +#75.75% 16.56MB + +class Solution: + def maximumOddBinaryNumber(self, s: str) -> str: + ones = s.count('1') + lis = [] + for i in range(ones): + lis.append('1') + for i in range(len(s) - ones): + lis.append('0') + st = toStr(lis) + val = int(st, 2) + while val % 2 == 0: + newStr = moveRightMost(lis) + st = toStr(lis) + val = int(st, 2) + return st + +def toStr(lis): + st = '' + for i in lis: + st += i + return st + +def moveRightMost(s): + count = len(s) - 1 + for i in reversed(s): + if i == '0' and s[count - 1] == '1': + s[count - 1] = '0' + s[count] = '1' + return s + count -= 1 + return s