681 Next Closest Time
Input: "19:34"
Output: "19:39"
Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later. It is not 19:33, because this occurs 23 hours and 59 minutes later.Input: "23:59"
Output: "22:22"
Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.from datetime import *
class Solution:
def nextClosestTime(self, time):
"""
:type time: str
:rtype: str
"""
digits = set(time)
next_time = datetime.strptime(time, "%H:%M")
while True:
next_time = next_time + timedelta(minutes=1)
extract = next_time.strftime("%H:%M")
if self.within(digits, set(extract)):
return extract
def within(self, seta, setb):
for i in setb:
if i not in seta:
return False
return TrueLast updated