Skip to content

3114. Latest Time You Can Obtain After Replacing Characters

  • Time: $O(1)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
 public:
  // Similar to 1736. Latest Time by Replacing Hidden Digits
  string findLatestTime(string s) {
    string ans = s;
    if (s[0] == '?')
      ans[0] = s[1] == '?' || s[1] < '2' ? '1' : '0';
    if (s[1] == '?')
      ans[1] = ans[0] == '1' ? '1' : '9';
    if (s[3] == '?')
      ans[3] = '5';
    if (s[4] == '?')
      ans[4] = '9';
    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
  // Similar to 1736. Latest Time by Replacing Hidden Digits
  public String findLatestTime(String s) {
    char[] ans = s.toCharArray();
    if (s.charAt(0) == '?')
      ans[0] = s.charAt(1) == '?' || s.charAt(1) < '2' ? '1' : '0';
    if (s.charAt(1) == '?')
      ans[1] = ans[0] == '1' ? '1' : '9';
    if (s.charAt(3) == '?')
      ans[3] = '5';
    if (s.charAt(4) == '?')
      ans[4] = '9';
    return new String(ans);
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution:
  # Similar to 1736. Latest Time by Replacing Hidden Digits
  def findLatestTime(self, s: str) -> str:
    ans = list(s)
    if s[0] == '?':
      ans[0] = '1' if s[1] == '?' or s[1] < '2' else '0'
    if s[1] == '?':
      ans[1] = '1' if ans[0] == '1' else '9'
    if s[3] == '?':
      ans[3] = '5'
    if s[4] == '?':
      ans[4] = '9'
    return ''.join(ans)