You are given a string s representing a 12-hour format time where some of the digits (possibly none) are replaced with a "?".
12-hour times are formatted as "HH:MM", where HH is between 00 and 11, and MM is between 00 and 59. The earliest 12-hour time is 00:00, and the latest is 11:59.
You have to replace all the "?" characters in s with digits such that the time we obtain by the resulting string is a valid 12-hour format time and is the latest possible.
Return the resulting string.
Example 1:
Input: s = "1?:?4"
Output: "11:54"
Explanation: The latest 12-hour format time we can achieve by replacing "?" characters is "11:54".
Example 2:
Input: s = "0?:5?"
Output: "09:59"
Explanation: The latest 12-hour format time we can achieve by replacing "?" characters is "09:59".
Constraints:
s.length == 5s[2] is equal to the character ":".s[2] are digits or "?" characters."00:00" and "11:59" that you can obtain after replacing the "?" characters.Problem Overview: You receive a time string in the format hh:mm where some characters are replaced with '?'. Each '?' can be replaced with a digit. The goal is to produce the latest valid time possible while respecting the constraints of the clock format.
The hours must form a valid value between 00 and 11, and minutes must be between 00 and 59. Because every unknown digit can be chosen freely, the strategy is to maximize the time while keeping the result valid.
Approach 1: Brute Force Simulation (Time: O(1), Space: O(1))
Enumerate every valid time from 11:59 down to 00:00 and check whether it matches the pattern of the input string. A time matches if each digit either equals the corresponding character or the input contains '?' at that position. Once a match is found, return it immediately because the search starts from the latest time.
This works because the total number of possibilities is very small (12 × 60 = 720). Even a full scan is constant time. The approach demonstrates the problem constraints clearly and relies on simple enumeration and string comparison.
Approach 2: Greedy Replacement (Time: O(1), Space: O(1))
The optimal solution replaces each '?' with the largest digit that still keeps the time valid. Start with the hour digits. If the first hour digit is '?', choose '1' when the second digit is '?' or ≤ '1'; otherwise choose '0'. This guarantees the hour does not exceed 11. If the second hour digit is '?', pick '1' when the first digit is '1', otherwise use '9' to maximize values like 09.
For the minutes, the constraints are simpler. Replace the first minute digit with '5' if it is '?', and the last digit with '9'. These are the largest values that keep minutes within 00–59.
The greedy method works because each position has a locally optimal choice that never blocks a larger valid time later. The logic operates directly on the string and performs a few conditional checks. This makes it both faster to implement and easier to reason about than scanning every candidate. The solution mainly involves manipulating a string and applying simple greedy decisions.
Recommended for interviews: The greedy replacement approach is what interviewers typically expect. It shows you understand the constraints of the time format and can derive the maximum valid digit for each position. Mentioning the brute force enumeration first demonstrates problem exploration, but implementing the greedy logic highlights stronger algorithmic thinking.
This approach uses greedy replacement strategy to replace each '?' in the input string. Start with the hour, maximizing its value by checking if the first digit can be 1 or 0. Then, move to the minute where the largest possible digits up to 5 and 9 are used for the tens and unit place respectively.
This C program defines a function that takes a time string and replaces '?' with appropriate digits to form the latest valid time. It statically allocates the result to preserve it after function exit. The function uses conditional checks based on the constraints provided.
Time Complexity: O(1) since the solution operates on a fixed size string.
Space Complexity: O(1) due to the static allocation of a fixed-size character array.
This approach involves enumerating all possible substitutions for '?' with a pre-constructed list of valid times within a 12-hour clock. By iterating through all valid possibilities, it picks the maximum valid time that fits the conditions.
This C code iterates through all potential hour and minute combinations within the valid 12-hour range, comparing each possibility to the input time string with constraints, and returns the maximal valid time found. This brute force approach offers a guaranteed solution with simplicity.
Time Complexity: O(1200) due to full simulation through 00:00 to 11:59.
Space Complexity: O(1) as a fixed number of variables and string buffers are utilized.
We can enumerate all times from large to small, where the hour h ranges from 11 to 0, and the minute m ranges from 59 to 0. For each time t, we check whether each digit of t matches the corresponding digit in s (if the corresponding digit in s is not "?"). If it does, then we have found the answer and return t.
The time complexity is O(h times m), where h = 12 and m = 60. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
We can judge each digit of s one by one. If it is "?", we determine the value of this digit based on the characters before and after it. Specifically, we have the following rules:
s[0] is "?", then the value of s[0] should be "1" or "0", depending on the value of s[1]. If s[1] is "?" or s[1] is less than "2", then the value of s[0] should be "1", otherwise the value of s[0] should be "0".s[1] is "?", then the value of s[1] should be "1" or "9", depending on the value of s[0]. If s[0] is "1", then the value of s[1] should be "1", otherwise the value of s[1] should be "9".s[3] is "?", then the value of s[3] should be "5".s[4] is "?", then the value of s[4] should be "9".The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Greedy Replacement | Time Complexity: O(1) since the solution operates on a fixed size string. |
| Brute Force Simulation | Time Complexity: O(1200) due to full simulation through 00:00 to 11:59. |
| Enumeration | — |
| Judge Each Digit | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(1) | O(1) | Useful for quickly validating the idea or when constraints are extremely small |
| Greedy Replacement | O(1) | O(1) | Best approach for interviews and production code; directly constructs the maximum valid time |
Leetcode weekly contest 393 || 100256.Latest Time You Can Obtain After Replacing Characters #contest • Ezy Codes • 633 views views
Watch 7 more video solutions →Practice Latest Time You Can Obtain After Replacing Characters with our built-in code editor and test cases.
Practice on FleetCode