Single-Row Keyboard

🏠 ⬅️ ➡️

There is a special keyboard with all keys in a single row.

Given a string keyboard of length 26 indicating the layout of the keyboard (indexed from 0 to 25). Initially, your finger is at index 0. To type a character, you have to move your finger to the index of the desired character. The time taken to move your finger from index i to index j is |i - j|.

You want to type a string word. Write a function to calculate how much time it takes to type it with one finger.

Example 1:

Input: keyboard = "abcdefghijklmnopqrstuvwxyz ", word = "cba " Output: 4 Explanation: The index moves from 0 to 2 to write 'c' then to 1 to write 'b' then to 0 again to write 'a'. Total time = 2 + 1 + 1 = 4.

Example 2:

Input: keyboard = "pqrstuvwxyzabcdefghijklmno ", word = "leetcode " Output: 73

Constraints:

  • keyboard.length == 26
  • keyboard contains each English lowercase letter exactly once in some order.
  • 1 <= word.length <= 104
  • word[i] is an English lowercase letter.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i, j, n, m
    character(len=26) :: keyboard
    character(len=*) :: word
    integer :: time

    ! read input
    read(*,*) keyboard
    read(*,*) word

    ! solve problem
    n = len_trim(word)
    m = len_trim(keyboard)
    time = 0
    do i = 1, n
        j = index(keyboard, word(i:i))
        time = time + abs(i - j)
    end do

    ! print output
    write(*,*) time

end program main
Compiled
Executed
Correct
module keyboard_module

  implicit none

contains

  function calculate_time(keyboard, word) result(time)
    character(len=*), intent(in) :: keyboard, word
    integer :: time
    integer :: i, j, k

    ! Initialize the time to 0
    time = 0

    ! Loop through each character in the word
    do i = 1, len_trim(word)

      ! Find the index of the current character in the keyboard
      do j = 1, len_trim(keyboard)
        if (word(i:i) == keyboard(j:j)) exit
      end do

      ! Calculate the time to move the finger from the current position to the new position
      k = abs(i - j)

      ! Add the time to the total time
      time = time + k

    end do

  end function calculate_time

end module keyboard_module

program main
  use keyboard_module
  implicit none

  character(len=26) :: keyboard = "abcdefghijklmnopqrstuvwxyz "
  character(len=*) :: word
  integer :: time

  ! Test case 1
  word = "cba "
  time = calculate_time(keyboard, word)
  write (*,*) "Time for 'cba ': ", time

  ! Test case 2
  word = "leetcode "
  time = calculate_time(keyboard, word)
  write (*,*) "Time for 'leetcode ': ", time

end program main
🌐 Data from online sources
def calculate_time(keyboard: str, word: str) -> int:
    time = 0
    prev_index = 0
    char_to_index = {char: i for i, char in enumerate(keyboard)}

    for c in word:
        time += abs(char_to_index[c] - prev_index)
        prev_index = char_to_index[c]

    return time
  1. Initialize the time to 0 and set the current position of the finger to index 0 of the keyboard.
  2. Convert the keyboard layout into a map (hashmap, dictionary, or object) with the characters as keys and their corresponding indices as values.
  3. Loop through each character in the given word and add the absolute difference between the previous index and the current index to the total time.
  4. Update the previous index to the index of the current character.
  5. Return the total time.
🌐 Data from online sources
int calculateTime(std::string keyboard, std::string word) {
    int time = 0;
    int prev_index = 0;
    std::unordered_map<char, int> char_to_index;

    for (int i = 0; i < keyboard.length(); i++) {
        char_to_index[keyboard[i]] = i;
    }

    for (char c : word) {
        time += std::abs(char_to_index[c] - prev_index);
        prev_index = char_to_index[c];
    }

    return time;
}
  1. Initialize the time to 0 and set the current position of the finger to index 0 of the keyboard.
  2. Convert the keyboard layout into a map (hashmap, dictionary, or object) with the characters as keys and their corresponding indices as values.
  3. Loop through each character in the given word and add the absolute difference between the previous index and the current index to the total time.
  4. Update the previous index to the index of the current character.
  5. Return the total time.