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
.program main
implicit none
character(len=5) :: time
integer :: answer
time = "?5:00 "
call solve(time, answer)
print '(I0)', answer
time = "0?:0? "
call solve(time, answer)
print '(I0)', answer
time = "??:?? "
call solve(time, answer)
print '(I0)', answer
contains
subroutine solve(time, answer)
implicit none
character(len=*), intent(in) :: time
integer, intent(out) :: answer
integer :: i, j, k, l, m
logical :: valid
do i = 0, 9
do j = 0, 9
do k = 0, 9
do l = 0, 9
do m = 0, 9
if (i == 0 .and. j == 0 .and. k == 0 .and. l == 0 .and. m == 0) then
cycle
end if
valid = .true.
if (time(1:1) == '?') then
if (i < 0 .or. i > 9) then
valid = .false.
end if
else
if (i /= int(time(1:1)) - 48) then
valid = .false.
end if
end if
if (valid) then
if (time(2:2) == '?') then
if (j < 0 .or. j > 9) then
valid = .false.
end if
else
if (j /= int(time(2:2)) - 48) then
valid = .false.
end if
end if
end if
if (valid) then
if (time(3:3) == '?') then
if (k < 0 .or. k > 9) then
valid = .false.
end if
else
if (k /= int(time(3:3)) - 48) then
valid = .false.
end if
end if
end if
if (valid) then
if (time(4:4) == '?') then
if (l < 0 .or. l > 9) then
valid = .false.
end if
else
if (l /= int(time(4:4)) - 48) then
valid = .false.
end if
end if
end if
if (valid) then
if (time(5:5) == '?') then
if (m < 0 .or. m > 9) then
valid = .false.
end if
else
if (m /= int(time(5:5)) - 48) then
valid = .false.
end if
end if
end if
if (valid) then
answer = answer + 1
end if
end do
end do
end do
end do
end do
end subroutine solve
end program main
temp.f95:45:45: 45 | if (i /= int(time(1:1)) - 48) then | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type temp.f95:56:49: 56 | if (j /= int(time(2:2)) - 48) then | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type temp.f95:68:49: 68 | if (k /= int(time(3:3)) - 48) then | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type temp.f95:80:49: 80 | if (l /= int(time(4:4)) - 48) then | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type temp.f95:92:49: 92 | if (m /= int(time(5:5)) - 48) then | 1 Error: βaβ argument of βintβ intrinsic at (1) must have a numeric type
module clock_times
implicit none
contains
integer function count_clock_times(time) result(answer)
character(len=*), intent(in) :: time
integer :: i, j, hh, mm
answer = 0
! Iterate over all possible values of hh and mm
do i = 0, 23
do j = 0, 59
! Convert hh and mm to strings
write(hh, '(i2.2)') i
write(mm, '(i2.2)') j
! Check if the current time is valid
if (is_valid_time(hh, mm, time)) then
answer = answer + 1
end if
end do
end do
end function count_clock_times
logical function is_valid_time(hh, mm, time) result(valid)
integer, intent(in) :: hh, mm
character(len=*), intent(in) :: time
valid = .false.
! Check if the current time is within the valid range
if (hh >= 0 .and. hh <= 23 .and. mm >= 0 .and. mm <= 59) then
! Check if the current time is a valid time
if (time(1:2) == hh .and. time(3:4) == mm) then
valid = .true.
end if
end if
end function is_valid_time
end module clock_times
program main
use clock_times
implicit none
integer :: answer
! Test case 1:
!answer = count_clock_times("?5:00 ")
!write (*,*) answer
! Test case 2:
!answer = count_clock_times("0?:0? ")
!write (*,*) answer
! Test case 3:
!answer = count_clock_times("??:?? ")
!write (*,*) answer
! Test case 4:
!answer = count_clock_times("00:00 ")
!write (*,*) answer
! Test case 5:
!answer = count_clock_times("23:59 ")
!write (*,*) answer
! Test case 6:
!answer = count_clock_times("00:00 ")
!write (*,*) answer
! Test case 7:
!answer = count_clock_times("23:59 ")
!write (*,*) answer
! Test case 8:
!answer = count_clock_times("00:00 ")
!write (*,*) answer
! Test case 9:
!answer = count_clock_times("23:59 ")
!write (*,*) answer
! Test case 10:
!answer = count_clock_times("00:00 ")
!write (*,*) answer
end program main
temp.f95:41:29: 41 | if (time(1:2) == hh .and. time(3:4) == mm) then | 1 Error: Operands of comparison operator β==β at (1) are CHARACTER(*)/INTEGER(4) temp.f95:52:5: 52 | use clock_times | 1 Fatal Error: Cannot open module file βclock_times.modβ for reading at (1): No such file or directory compilation terminated.
def count_valid_times(time: str) -> int:
count = 1
if time[0] == '?':
count *= 3 if time[1] == '?' or time[1] < '4' else 2
if time[1] == '?':
count *= 4 if time[0] == '2' else 10
if time[3] == '?':
count *= 6
if time[4] == '?':
count *= 10
return count
1. If the first character is '?', check if the second character is also '?' or less than '4', in that case the valid possibilities for the first character are '0', '1' or '2', so we multiply our count by 3. If the second character is greater than or equal to '4', then the valid possibilities for the first character are only '0' or '1', so we multiply our count by 2.
At the end, return the count
value which represents the number of valid clock times that can be created by replacing '?' with a digit from 0 to 9.
int countValidTimes(std::string time) {
int count = 1;
if(time[0] == '?') {
count *= (time[1] == '?' || time[1] < '4') ? 3 : 2;
}
if(time[1] == '?') {
count *= (time[0] == '2') ? 4 : 10;
}
if(time[3] == '?') {
count *= 6;
}
if(time[4] == '?') {
count *= 10;
}
return count;
}
1. If the first character is '?', check if the second character is also '?' or less than '4', in that case the valid possibilities for the first character are '0', '1' or '2', so we multiply our count by 3. If the second character is greater than or equal to '4', then the valid possibilities for the first character are only '0' or '1', so we multiply our count by 2.
At the end, return the count
value which represents the number of valid clock times that can be created by replacing '?' with a digit from 0 to 9.