There is a special typewriter with lowercase English letters 'a'
to 'z'
arranged in a circle with a pointer. A character can only be typed if the pointer is pointing to that character. The pointer is initially pointing to the character 'a'
.
Each second, you may perform one of the following operations:
Given a string word
, return the minimum number of seconds to type out the characters in word
.
Example 1:
Input: word = "abc " Output: 5 Explanation: The characters are printed as follows: - Type the character 'a' in 1 second since the pointer is initially on 'a'. - Move the pointer clockwise to 'b' in 1 second. - Type the character 'b' in 1 second. - Move the pointer clockwise to 'c' in 1 second. - Type the character 'c' in 1 second.
Example 2:
Input: word = "bza " Output: 7 Explanation: The characters are printed as follows: - Move the pointer clockwise to 'b' in 1 second. - Type the character 'b' in 1 second. - Move the pointer counterclockwise to 'z' in 2 seconds. - Type the character 'z' in 1 second. - Move the pointer clockwise to 'a' in 1 second. - Type the character 'a' in 1 second.
Example 3:
Input: word = "zjpc " Output: 34 Explanation: The characters are printed as follows: - Move the pointer counterclockwise to 'z' in 1 second. - Type the character 'z' in 1 second. - Move the pointer clockwise to 'j' in 10 seconds. - Type the character 'j' in 1 second. - Move the pointer clockwise to 'p' in 6 seconds. - Type the character 'p' in 1 second. - Move the pointer counterclockwise to 'c' in 13 seconds. - Type the character 'c' in 1 second.
Constraints:
1 <= word.length <= 100
word
consists of lowercase English letters.program main
! Solves the problem described in the task
implicit none
integer :: i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
integer :: num_seconds
character(len=1) :: word
! Define the special typewriter
data (i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)/'a', 'b', 'c', &
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', &
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'/
! Initialize the pointer
data i /'a'/
! Read the input string
read(*,'(A)') word
! Loop over each character in the input string
do j=1,len(word)
select case(word(j:j))
! If the character is 'a' to 'z', move the pointer accordingly
case('a':'z')
if (word(j:j) == i) then
! If the pointer is already pointing to the current character, type it
write(*,'(I1)') 1
else
! Otherwise, move the pointer to the current character and type it
i = word(j:j)
write(*,'(I1)') 1
end if
! If the character is ' ', skip it
case(' ')
continue
! If the character is not recognized, error
case default
write(*,'(A)') 'Error: Unrecognized character.'
stop
end select
end do
end program main
temp.f95:10:63: 10 | data (i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)/'a', 'b', 'c', & | 1 Error: Syntax error in DATA statement at (1) temp.f95:15:11: 15 | data i /'a'/ | 1 Error: Incompatible types in DATA statement at (1); attempted conversion of CHARACTER(1) to INTEGER(4) temp.f95:25:20: 25 | if (word(j:j) == i) then | 1 Error: Operands of comparison operator ‘==’ at (1) are CHARACTER(*)/INTEGER(4) temp.f95:30:24: 30 | i = word(j:j) | 1 Error: Cannot convert CHARACTER(*) to INTEGER(4) at (1)
module typewriter
implicit none
integer, parameter :: n = 26 ! number of characters in the alphabet
integer, parameter :: m = 10 ! number of seconds to move the pointer clockwise or counterclockwise
integer :: pointer = 1 ! initial position of the pointer
integer :: seconds = 0 ! total number of seconds
contains
subroutine type_word(word)
character(len=*), intent(in) :: word
integer :: i ! loop variable
do i = 1, len(word)
! move the pointer clockwise or counterclockwise
if (word(i:i) == 'L') then
pointer = mod(pointer - 1, n) + 1
seconds = seconds + m
else if (word(i:i) == 'R') then
pointer = mod(pointer + 1, n) + 1
seconds = seconds + m
end if
! type the character
write (*, '(A)') word(i:i)
seconds = seconds + 1
end do
end subroutine type_word
end module typewriter
program main
use typewriter
implicit none
character(len=100) :: word ! input word
! test cases
call type_word('abc') ! 5 seconds
call type_word('bza') ! 7 seconds
call type_word('zjpc') ! 34 seconds
end program main
a b c b z a z j p c
def minTimeToType(word: str) -> int:
time = 0
position = 0
for c in word:
next_position = ord(c) - ord('a')
diff = abs(next_position - position)
time += min(diff, 26 - diff) + 1
position = next_position
return time
int minTimeToType(string word) {
int time = 0, position = 0;
for (char c : word) {
int next_position = c - 'a';
int diff = abs(next_position - position);
time += min(diff, 26 - diff) + 1;
position = next_position;
}
return time;
}