You are given a string of length 5
that represents the time in the format "HH:MM"
. The string can contain digits and/or the character '?'
which can represent any digit from 0
to 9
. You need to return the total number of valid times the string can represent.
A valid time is in the range from "00:00"
to "23:59"
.
"?"
will appear outside the HH:MM
format?
"HH:MM"
and will always be of length 5. The ?
will only appear in place of digits."??:??"
?
def countTime(time: str) -> int:
count = 0
for hour in range(24):
for minute in range(60):
hh = f'{hour:02d}'
mm = f'{minute:02d}'
valid = True
for i in range(2):
if time[i] != '?' and time[i] != hh[i]:
valid = False
break
for i in range(3, 5):
if time[i] != '?' and time[i] != mm[i - 3]:
valid = False
break
if valid:
count += 1
return count
# Example usage:
time = "1?:45"
print(countTime(time)) # Output would depend on the example
We iterate efficiently checking each specific valid time and determine if they align with the provided pattern.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?