array and hashmap
November 7, 2022
Maximum 69 number
We will iterate over all the digits of the number and check if the digit is 6. If it is, we will replace it with 9 and return the number.
class Solution:
def maximum69Number (self, num: int) -> int:
num = list(str(num))
for i in range(len(num)):
if num[i] == '6':
num[i] = '9'
break
return int(''.join(num))
Time complexity: O(n)
Space complexity: O(n)
Here is a one-liner solution:
class Solution:
def maximum69Number (self, num: int) -> int:
return int(str(num).replace('6', '9', 1))