Problem statement

https://leetcode.com/problems/next-closest-time/

Solution

Just traverse all next times and check if they are OK.

Complexity

Time complexity in worst case is O(24*60), space is O(1).

Code

class Solution:
    def nextClosestTime(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