Latest Time by Replacing Hidden Digits - String - Easy - LeetCode
💻 coding

Latest Time by Replacing Hidden Digits - String - Easy - LeetCode

1 min read 188 words
1 min read
ShareWhatsAppPost on X
  • 1The problem involves replacing hidden digits in a time string to form the latest valid time.
  • 2Valid times range from 00:00 to 23:59, and examples illustrate how to derive the latest time.
  • 3The solution uses a character array to replace '?' with appropriate digits based on the hour and minute constraints.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The problem involves replacing hidden digits in a time string to form the latest valid time."

Latest Time by Replacing Hidden Digits - String - Easy - LeetCode

You are given a string time in the form of hh:mm, where some of the digits in the string are hidden (represented by ?).

The valid times are those inclusively between 00:00 and 23:59.

Return the latest valid time you can get from time by replacing the hidden digits.

Example 1:

Input: time = "2?:?0" Output: "23:50" Explanation: The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50. Example 2:

Input: time = "0?:3?" Output: "09:39" Example 3:

Input: time = "1?:22" Output: "19:22"

Constraints:

time is in the format hh:mm. It is guaranteed that you can produce a valid time from the given string.

public class Solution {
 public string MaximumTime(string time) { 
 var times = time.ToCharArray();
 if(times[0] == '?') {
 times[0] = times[1] <= '3' || times[1] == '?' ? '2' : '1';
 }
 
 if(times[1] == '?') {
 times[1] = times[0] == '2' ? '3' : '9';
 }
 
 if(times[3] == '?') {
 times[3] = '5';
 }
 
 if(times[4] == '?') {
 times[4] = '9';
 }
 
 return new String(times);
 }
}

Time Complexity: O(1)

Space Complexity: O(1)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

sumitc91

Published on 26 January 2021 · 1 min read · 188 words

Part of AskGif Blog · coding

You might also like