-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0681-next-closest-time.js
More file actions
50 lines (42 loc) · 1.4 KB
/
0681-next-closest-time.js
File metadata and controls
50 lines (42 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 681. Next Closest Time
* https://leetcode.com/problems/next-closest-time/
* Difficulty: Medium
*
* Given a time represented in the format "HH:MM", form the next closest time by reusing the
* current digits. There is no limit on how many times a digit can be reused.
*
* You may assume the given input string is always valid. For example, "01:34", "12:09" are
* all valid. "1:34", "12:9" are all invalid.
*/
/**
* @param {string} time
* @return {string}
*/
var nextClosestTime = function(time) {
const digits = new Set(time.replace(':', ''));
const sortedDigits = [...digits].sort();
return generateNextTime(time);
function isValidTime(h, m) {
return h < 24 && m < 60;
}
function generateNextTime(timeStr) {
const [hours, minutes] = timeStr.split(':');
for (let i = 3; i >= 0; i--) {
const pos = i < 2 ? i : i + 1;
const currentDigit = timeStr[pos];
for (const digit of sortedDigits) {
if (digit > currentDigit) {
const newTime = timeStr.substring(0, pos) + digit + timeStr.substring(pos + 1);
const [h, m] = newTime.split(':').map(Number);
if (isValidTime(h, m)) {
return newTime;
}
}
}
const newTime = timeStr.substring(0, pos) + sortedDigits[0] + timeStr.substring(pos + 1);
timeStr = newTime;
}
return sortedDigits[0].repeat(2) + ':' + sortedDigits[0].repeat(2);
}
};