[
string
]
BinarySearch 0432 Weird Clock
Problem statement
https://binarysearch.com/problems/Weird-Clock/
Solution
Equal to Leetcode 0681 Next Closest Time.
Complexity
It is O(24*60)
for time and O(1)
for space.
Code
class Solution:
def solve(self, time):
digits = set(time)
time = int(time[:2])*60 + int(time[3:])
for next_time in range(time + 1, time + 60*24 + 1):
if next_time >= 60*24: next_time -= 60*24
mins, hours = next_time % 60, next_time // 60
mins = str(mins + 100)[1:]
hours = str(hours + 100)[1:]
time_str = hours + ":" + mins
if not set(time_str) - digits: return time_str