2437. Number of Valid Clock Times LeetCode Solution
In this guide, you will get 2437. Number of Valid Clock Times LeetCode Solution with the best time and space complexity. The solution to Number of Valid Clock Times problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Number of Valid Clock Times solution in C++
- Number of Valid Clock Times solution in Java
- Number of Valid Clock Times solution in Python
- Additional Resources
Problem Statement of Number of Valid Clock Times
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"
Some of the digits might be replaced with '?' and need to be replaced with digits from 0 to 9.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
2437. Number of Valid Clock Times LeetCode Solution in C++
class Solution {
public:
int countTime(string time) {
int ans = 1;
if (time[3] == '?')
ans *= 6;
if (time[4] == '?')
ans *= 10;
if (time[0] == '?' && time[1] == '?')
return ans * 24;
if (time[0] == '?')
return time[1] < '4' ? ans * 3 : ans * 2;
if (time[1] == '?')
return time[0] == '2' ? ans * 4 : ans * 10;
return ans;
}
};
/* code provided by PROGIEZ */
2437. Number of Valid Clock Times LeetCode Solution in Java
class Solution {
public int countTime(String time) {
int ans = 1;
if (time.charAt(3) == '?')
ans *= 6;
if (time.charAt(4) == '?')
ans *= 10;
if (time.charAt(0) == '?' && time.charAt(1) == '?')
return ans * 24;
if (time.charAt(0) == '?')
return time.charAt(1) < '4' ? ans * 3 : ans * 2;
if (time.charAt(1) == '?')
return time.charAt(0) == '2' ? ans * 4 : ans * 10;
return ans;
}
}
// code provided by PROGIEZ
2437. Number of Valid Clock Times LeetCode Solution in Python
class Solution:
def countTime(self, time: str) -> int:
ans = 1
if time[3] == '?':
ans *= 6
if time[4] == '?':
ans *= 10
if time[0] == '?' and time[1] == '?':
return ans * 24
if time[0] == '?':
return ans * 3 if time[1] < '4' else ans * 2
if time[1] == '?':
return ans * 4 if time[0] == '2' else ans * 10
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.