You are given a string time
in the form of hh:mm
, where some of the digits in the string are hidden (represented by ?
).
The valid times are those inclusively between 00:00
and 23:59
.
Return the latest valid time you can get from time
by replacing the hidden digits.
Example 1:
Input: time = "2?:?0 " Output: "23:50 " Explanation: The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.
Example 2:
Input: time = "0?:3? " Output: "09:39 "
Example 3:
Input: time = "1?:22 " Output: "19:22 "
Constraints:
time
is in the format hh:mm
.program main
implicit none
character(len=8) :: time
integer :: hh, mm
time = "2?:?0"
call solve(time, hh, mm)
write (*, "(A, I2, A, I2)") "Latest valid time from ", time, " is ", hh, ":", mm
time = "0?:3?"
call solve(time, hh, mm)
write (*, "(A, I2, A, I2)") "Latest valid time from ", time, " is ", hh, ":", mm
time = "1?:22"
call solve(time, hh, mm)
write (*, "(A, I2, A, I2)") "Latest valid time from ", time, " is ", hh, ":", mm
contains
subroutine solve(time, hh, mm)
implicit none
character(len=*), intent(in) :: time
integer, intent(out) :: hh, mm
integer :: i, j
logical :: found
do i = 1, 2
if (time(i:i) == '?') then
do j = 0, 9
if (j /= 0 .and. j /= 6) then
time(i:i) = char(ichar('0') + j)
found = .true.
exit
end if
end do
if (.not. found) then
stop "No solution"
end if
end if
end do
read (time, "(I2, A, I2)") hh, ":", mm
end subroutine solve
end program main
temp.f95:44:39: 44 | read (time, "(I2, A, I2)") hh, ":", mm | 1 Error: Expecting variable in READ statement at (1) temp.f95:33:24: 33 | time(i:i) = char(ichar('0') + j) | 1 Error: Dummy argument βtimeβ with INTENT(IN) in variable definition context (assignment) at (1)
PROGRAM main
IMPLICIT NONE
CHARACTER(len=8) :: time
CHARACTER(len=2) :: hour
CHARACTER(len=2) :: minute
INTEGER :: h, m
! Read input
READ(*,*) time
! Extract hour and minute
hour = time(1:2)
minute = time(4:5)
! Convert to integers
READ(hour,*) h
READ(minute,*) m
! Check if time is valid
IF (h < 24 .AND. m < 60) THEN
! Print latest valid time
WRITE(*,*) hour, minute
ELSE
! Print error message
WRITE(*,*) "Invalid time"
END IF
END PROGRAM main
At line 11 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7b69d0c80960 in ??? #1 0x7b69d0c814d9 in ??? #2 0x7b69d0ed517b in ??? #3 0x7b69d0ece684 in ??? #4 0x7b69d0ecf2aa in ??? #5 0x55e19a109219 in MAIN__ #6 0x55e19a10948c in main
def maximumTime(time: str) -> str:
time_list = list(time)
if time_list[0] == '?':
time_list[0] = '2' if time_list[1] <= '3' or time_list[1] == '?' else '1'
if time_list[1] == '?':
time_list[1] = '3' if time_list[0] == '2' else '9'
if time_list[3] == '?':
time_list[3] = '5'
if time_list[4] == '?':
time_list[4] = '9'
return "".join(time_list)
The algorithm uses the following steps: 1. For each character in the input string, check if it's a "?". 2. If the character is "?", assign a valid hour or minute value depending on its position in the string and other characters' values. - If the first "?" is at position 0, check if the character at position 1 is less than or equal to 3 or "?". If true, assign '2' else, assign '1' to position 0. - If the first "?" is at position 1, check if the character at position 0 is '2'. If true, assign '3' else, assign '9' to position 1. - If the first "?" is at position 3, assign '5' to it, any value between 00-59 is valid. - If the first "?" is at position 4, assign '9' to it, any value between 00-59 is valid.
Finally, return the modified string.
#include <string>
std::string maximumTime(std::string time) {
if (time[0] == '?') {
time[0] = (time[1] <= '3' || time[1] == '?') ? '2' : '1';
}
if (time[1] == '?') {
time[1] = (time[0] == '2') ? '3' : '9';
}
if (time[3] == '?') {
time[3] = '5';
}
if (time[4] == '?') {
time[4] = '9';
}
return time;
}
The algorithm uses the following steps: 1. For each character in the input string, check if it's a "?". 2. If the character is "?", assign a valid hour or minute value depending on its position in the string and other characters' values. - If the first "?" is at position 0, check if the character at position 1 is less than or equal to 3 or "?". If true, assign '2' else, assign '1' to position 0. - If the first "?" is at position 1, check if the character at position 0 is '2'. If true, assign '3' else, assign '9' to position 1. - If the first "?" is at position 3, assign '5' to it, any value between 00-59 is valid. - If the first "?" is at position 4, assign '9' to it, any value between 00-59 is valid.
Finally, return the modified string.