Minimum Time to Type Word Using Special Typewriter

🏠 ⬅️ ➡️

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:

  • Move the pointer one character counterclockwise or clockwise.
  • Type the character the pointer is currently on.

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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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
  1. Initialize the time and position to 0.
  2. Iterate through the characters of the word.
  3. Calculate the next position based on the ASCII value of the character and 'a'.
  4. Calculate the difference between the next position and the current position.
  5. Update the time by adding the minimum of the difference and (26 - difference), and 1 extra second for typing the character.
  6. Update the current position to the next position.
  7. Return the total time after processing all characters in the word.
🌐 Data from online sources
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;
}
  1. Initialize the time and position to 0.
  2. Iterate through the characters of the word.
  3. Calculate the next position based on the ASCII value of the character and 'a'.
  4. Calculate the difference between the next position and the current position.
  5. Update the time by adding the minimum of the difference and (26 - difference), and 1 extra second for typing the character.
  6. Update the current position to the next position.
  7. Return the total time after processing all characters in the word.