Program to find latest valid time by replacing hidden digits in Python

Suppose we have a string representing time in the format hh:mm. Some digits are hidden (represented by ?). Considering a 24-hour clock, valid times are between 00:00 and 23:59. We need to find the latest valid time by replacing the hidden digits.

For example, if the input is "1?:?5", the output will be "19:55" as we want the latest possible time.

Algorithm

To solve this problem, we need to consider the constraints of a 24-hour time format ?

  • First digit of hour: can be 0, 1, or 2

  • Second digit of hour: depends on the first digit

  • First digit of minute: can be 0-5

  • Second digit of minute: can be 0-9

Example

Let's implement the solution step by step ?

def find_latest_time(s):
    time_chars = list(s)
    
    # Handle first digit of hour (position 0)
    if time_chars[0] == '?':
        time_chars[0] = '2'  # Latest possible
    
    # Handle second digit of hour (position 1)
    if time_chars[1] == '?':
        if time_chars[0] == '2':
            time_chars[1] = '3'  # 23 is max when first digit is 2
        else:
            time_chars[1] = '9'  # 09, 19 are max when first digit is 0 or 1
    
    # Adjust first digit if second digit makes it invalid
    if time_chars[0] == '?' and time_chars[1] > '3':
        time_chars[0] = '1'
    
    # Handle first digit of minute (position 3)
    if time_chars[3] == '?':
        time_chars[3] = '5'  # Latest possible (59 minutes max)
    
    # Handle second digit of minute (position 4)
    if time_chars[4] == '?':
        time_chars[4] = '9'  # Latest possible
    
    return ''.join(time_chars)

# Test with the example
s = "1?:?5"
result = find_latest_time(s)
print(f"Input: {s}")
print(f"Output: {result}")
Input: 1?:?5
Output: 19:55

More Examples

Let's test with different cases to understand the logic better ?

def find_latest_time(s):
    time_chars = list(s)
    
    # Handle first digit of hour
    if time_chars[0] == '?':
        time_chars[0] = '2'
    
    # Handle second digit of hour
    if time_chars[1] == '?':
        if time_chars[0] == '2':
            time_chars[1] = '3'
        else:
            time_chars[1] = '9'
    
    # Handle first digit of minute
    if time_chars[3] == '?':
        time_chars[3] = '5'
    
    # Handle second digit of minute
    if time_chars[4] == '?':
        time_chars[4] = '9'
    
    return ''.join(time_chars)

# Test cases
test_cases = ["??:??", "2?:??", "?4:??", "??:?0", "1?:30"]

for test in test_cases:
    result = find_latest_time(test)
    print(f"{test} ? {result}")
??:?? ? 23:59
2?:?? ? 23:59
?4:?? ? 14:59
??:?0 ? 23:50
1?:30 ? 19:30

How It Works

The algorithm follows these rules ?

  • Hour constraints: Maximum valid hour is 23, so if first digit is 2, second digit can be at most 3

  • Minute constraints: Maximum valid minute is 59, so first digit can be at most 5

  • Greedy approach: Always choose the largest possible digit to get the latest time

Key Points

Position Maximum Value Condition
Hour[0] 2 Always
Hour[1] 3 or 9 3 if Hour[0] = 2, else 9
Minute[0] 5 Always
Minute[1] 9 Always

Conclusion

This greedy approach ensures we get the latest valid time by maximizing each digit based on 24-hour format constraints. The key is understanding that hour digits have interdependent constraints while minute digits are independent.

Updated on: 2026-03-25T20:34:06+05:30

325 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements