Skip to content

1736. Latest Time by Replacing Hidden Digits

  • Time: $O(1)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
 public:
  string maximumTime(string time) {
    string ans = time;
    if (time[0] == '?')
      ans[0] = time[1] == '?' || time[1] < '4' ? '2' : '1';
    if (time[1] == '?')
      ans[1] = ans[0] == '2' ? '3' : '9';
    if (time[3] == '?')
      ans[3] = '5';
    if (time[4] == '?')
      ans[4] = '9';
    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
  public String maximumTime(String time) {
    char[] ans = time.toCharArray();
    if (time.charAt(0) == '?')
      ans[0] = time.charAt(1) == '?' || time.charAt(1) < '4' ? '2' : '1';
    if (time.charAt(1) == '?')
      ans[1] = ans[0] == '2' ? '3' : '9';
    if (time.charAt(3) == '?')
      ans[3] = '5';
    if (time.charAt(4) == '?')
      ans[4] = '9';
    return new String(ans);
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution:
  def maximumTime(self, time: str) -> str:
    ans = list(time)
    if time[0] == '?':
      ans[0] = '2' if time[1] == '?' or time[1] < '4' else '1'
    if time[1] == '?':
      ans[1] = '3' if ans[0] == '2' else '9'
    if time[3] == '?':
      ans[3] = '5'
    if time[4] == '?':
      ans[4] = '9'
    return ''.join(ans)