Latest Time by Replacing Hidden Digits LeetCode solution in Java

1
class Solution {
2
public String maximumTime(String time) {
3
char[] times = time.toCharArray();
4
// for times[0]
5
// if both characters of hour is ?, then hour is 23 then times[0] should be '2'("??:3?)".
6
// if 2nd character of hour is <= 3, then hour can be in 20s then times[0] should be '2'.
7
// if 2nd character of hour is >3, then hour can only be in 10s then times[0] should be '1'.
8
if (times[0] == '?') times[0] = (times[1] <= '3' || times[1] == '?') ? '2' : '1';
9
// if 1st character of hour is 0 or 1, then hour can be 09 or 19 then times[1] should be '9'.
10
// if 1st character of hour is 2, then hour can be 23 then times[1] should be '3'.
11
if (times[1] == '?') times[1] = times[0] == '2' ? '3' : '9';
12
// if both characters of minute is ? then minute is 59, or only 4th character is ? then 5_ so
13
// times[3] is always '5'.
14
if (times[3] == '?') times[3] = '5';
15
// if 2nd character of minute is ?, then times[4] is '9'.
16
if (times[4] == '?') times[4] = '9';
17
return new String(times);
18
}
19
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0