# Python with explanation, very straightforward, beats 100%

• We need to use only provided digits, so it is better to manipulate with digits indexes and not the digits itself. To do so we need a sorted list of unique digits.
Check for corner cases: 60 minutes and 24 hours.
The smallest time in a day has all four indexes equal 0.
Solution is very straightforward and beats 100% Python solutions with 39 ms.

``````def nextClosestTime(self, time):
ctime = time[0:2] + time[3:5] # split time into an array of chars
d = sorted(list(set(ctime))) # sorted list of unique digits
i = list(map(d.index, ctime)) # indexes of digits in time
n = len(d) # number of digits
i[3] += 1
if i[3] >= n:
i[3] = 0
i[2] += 1
if i[2] >= n or d[i[2]] >= '6':
i[2] = 0
i[1] += 1
if i[1] >= n or d[i[0]] == '2' and d[i[1]] >= '4':
i[1] = 0
i[0] += 1
if i[0] >= n or i[1] >= n or d[i[0]] >= '3' or d[i[0]] == '2' and d[i[1]] >= '4':
i[0] = 0

return d[i[0]] + d[i[1]] + ':' + d[i[2]] + d[i[3]]``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.