Skip to main content

Latest Time You Can Obtain After Replacing Characters - Solution & Explanation

EasyStringEnumeration21 min readAsked at: Google
Practice this problem

Problem Statement

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 == 5
  • s[2] is equal to the character ":".
  • All characters except s[2] are digits or "?" characters.
  • The input is generated such that there is at least one time between "00:00" and "11:59" that you can obtain after replacing the "?" characters.

Approach Overview

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.

Approach 1: Greedy Replacement

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Brute Force Simulation

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 3: Enumeration

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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Approach 4: Judge Each Digit

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:

  • If 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".
  • If 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".
  • If s[3] is "?", then the value of s[3] should be "5".
  • If 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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Greedy Replacement

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.

Brute Force Simulation

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.

Enumeration
Judge Each Digit

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SimulationO(1)O(1)Useful for quickly validating the idea or when constraints are extremely small
Greedy ReplacementO(1)O(1)Best approach for interviews and production code; directly constructs the maximum valid time

Video Solution

Leetcode weekly contest 393 || 100256.Latest Time You Can Obtain After Replacing Characters #contestEzy Codes633 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Latest Time You Can Obtain After Replacing Characters easy or hard?
Latest Time You Can Obtain After Replacing Characters is classified as an Easy problem. The challenge is understanding the valid hour range and selecting the maximum digit that keeps the time valid. Once those constraints are clear, the greedy implementation is straightforward.
Latest Time You Can Obtain After Replacing Characters Python/Java solution
The greedy approach translates directly to Python, Java, C++, C#, JavaScript, and C. Convert the string to a mutable structure, apply conditional replacements for hour and minute digits, then return the updated string. The implementation typically takes only a few lines.
How to solve Latest Time You Can Obtain After Replacing Characters in O(1)?
Process the string digit by digit and greedily assign the largest valid value. Ensure the hour stays within 00–11 by choosing '1' or '0' for the first digit depending on the second. Fill remaining unknowns with the maximum valid digits, such as '5' and '9' for minutes.
What is the best approach for Latest Time You Can Obtain After Replacing Characters?
Greedy replacement is the best approach. Replace each '?' with the largest possible digit that still keeps the time valid. Handle hour constraints first (00–11), then minutes (00–59). The algorithm runs in O(1) time because the string length is fixed.
Is Latest Time You Can Obtain After Replacing Characters asked at Google/Amazon/Meta?
The problem resembles common interview questions about time formatting, greedy digit selection, and string manipulation. Variants of these problems appear in interviews at companies like Amazon and Google where candidates must reason about constraints and edge cases.
What data structure is used in Latest Time You Can Obtain After Replacing Characters?
The solution mainly uses simple string manipulation. Characters are inspected and replaced directly in the time string while applying greedy rules. No advanced data structures are required beyond basic string or character arrays.
What is the time complexity of Latest Time You Can Obtain After Replacing Characters?
Both common solutions run in constant time. The greedy solution performs a few conditional checks on a fixed-length string, giving O(1) time and O(1) space. Even the brute force method checks at most 720 possible times, which is also constant.

Ready to solve this problem?

Practice Latest Time You Can Obtain After Replacing Characters with our built-in code editor and test cases.

Practice on FleetCode