You are given a string of length 5 called time, representing the current time on a digital clock in the format "hh:mm". The earliest possible time is "00:00" and the latest possible time is "23:59".
In the string time, the digits represented by the ? symbol are unknown, and must be replaced with a digit from 0 to 9.
Return an integer answer, the number of valid clock times that can be created by replacing every ? with a digit from 0 to 9.
Example 1:
Input: time = "?5:00" Output: 2 Explanation: We can replace the ? with either a 0 or 1, producing "05:00" or "15:00". Note that we cannot replace it with a 2, since the time "25:00" is invalid. In total, we have two choices.
Example 2:
Input: time = "0?:0?" Output: 100 Explanation: Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices.
Example 3:
Input: time = "??:??" Output: 1440 Explanation: There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 * 60 = 1440 choices.
Constraints:
time is a valid string of length 5 in the format "hh:mm"."00" <= hh <= "23""00" <= mm <= "59"'?' and need to be replaced with digits from 0 to 9.In this approach, we iterate over all possible valid hours and minutes, checking if a given configuration of the time string with '?' replaced can form a valid time. The constraints allow us to simply enumerate the values and check against the string.
This Python solution iterates through all potential hours (0 through 23) and minutes (0 through 59). For each hour-minute combination, it formats them into strings 'hh' and 'mm'. It then checks if each character matches the corresponding character in the given time string or if the character is '?', which can match any digit. If all positions match or are '?', this configuration of time is considered valid, and the count is incremented.
C++
Java
C#
JavaScript
C
The time complexity is O(1) since there are at most 24*60 = 1440 iterations. The space complexity is O(1) as we use only a few variables to store numbers and strings.
This approach simplifies the task by considering each position of '?' in the string individually, providing specific potential values given constraints (e.g., for 'hh', values depend on `time[0]`).
The Python optimized solution calculates the number of valid replacements by checking each character in 'time'. Depending on whether a character is '?', it multiplies the total possible number of ways by the valid number of digits appropriate for that character.
C++
Java
C#
JavaScript
C
Time Complexity: O(1), due to the constant number of checks and multiplicative operations. Space Complexity: O(1) as only a few integer calculations are involved.
| Approach | Complexity |
|---|---|
| Brute Force Enumeration | The time complexity is O(1) since there are at most 24*60 = 1440 iterations. The space complexity is O(1) as we use only a few variables to store numbers and strings. |
| Character-Specific Enumeration | Time Complexity: O(1), due to the constant number of checks and multiplicative operations. Space Complexity: O(1) as only a few integer calculations are involved. |
Valid Anagram - Leetcode 242 - Python • NeetCode • 660,685 views views
Watch 9 more video solutions →Practice Number of Valid Clock Times with our built-in code editor and test cases.
Practice on FleetCode