Calculate time required to type a word using given single-row keyboard


The following article discusses an efficient approach to compute the total time taken to type out a word using a single-row keyboard. This is an interesting problem and has been asked previously in technical interviews.

Problem Statement

Consider a hypothetical scenario wherein all the keys on a keyboard are placed in a single row. The first key is indexed 0 and the last key is indexed 25.

For a given string ‘s’ calculate the time taken to type all the characters of ‘s’ on a special keyboard with layout specified as ‘keyboard_layout’.

The time taken to move from one key to its adjacent key requires unit time. Traversal is possible in both directions. Initially we are at index 0.

Example

Input

keyboard = "mnbvcxzqwertyuioplkjhgfdsa", word = "cat"

Output

39

Explanation

Initially we are at index 0 i.e. m

To get to ‘c’, time taken = 4 since m>n>b>v>c

To get to ‘a’, time taken = 21 since c>x>z>q>w>e>r>t>y>u>i>o>p>k>j>h>g>f>d>s>a

To get to ‘t’, time taken = 14 since a>s>d>f>g>h>j>k>l>p>o>i>u>y>t

Total time = 4 + 21 + 14 = 39

Input

keyboard “rfvcdewsxzaqtgbnhyujmkiolp”, word = “hat”

Output

24

Explanation

To get to ‘h’, time taken = 16

To get to ‘a’, time taken = 6

To get to ‘t’, time taken = 2

Same logic followed as above.

Total time = 16 + 6 + 2 = 24

Input

keyboard = “plmkoijnbhuygvcftrdxzsewaq”, word = “bat”

Output

32

Explanation

To get to ‘b’, time taken = 8

To get to ‘a’, time taken = 16

To get to ‘t’, time taken = 8

Same logic followed as above.

Total time = 8 + 16 + 8 = 24

Solution Approach

To find the total time taken to type out a given word on a special single line keyboard with layout given as keyboard_layout, we first store the word to be typed and the keyboard_layout in 2 string variables.

We maintain the previous position and the current position of our finger. The time taken to type out each character is given by |curr_pos - prev_pos| and is added to the total time in each iteration. After each iteration we update prev_pos to the curr_pos.

Pseudocode

  • Start

  • Read the input strings keyboard and word from the user

  • Set time = 0 and prev_pos = 0

  • for each character c in word do the following:

    • Set curr_pos = keyboard.find(c)

    • Set time = time + abs(curr_pos - prev_pos)

    • Set prev_pos = curr_pos

  • Output the time as the total word typing time.

  • End

Dry Run

keyboard = "qazwsxedcrfvtgbyhnujmikolp"
word = "well"
1. time = 0, prev_pos = 0
2. For each character in word:
   - For 'w':
      - curr_pos = 0
      - time += abs(3 - 0) = 3
      - prev_pos = 3
   - For 'e':
      - curr_pos = 3
      - time += abs(6 - 3) = 3
      - prev_pos = 6
   - For 'l':
      - curr_pos = 24
      - time += abs(24 - 6) = 18
      - prev_pos = 24
   - For 'l':
      - curr_pos = 24
      - time += abs(24 - 24) = 0
      - prev_pos = 24
3. Return time = 3 + 3 + 18 + 0 = 24

Example: C++ Program

The program takes as input a string keyboard_layout representing the layout of a special keyboard with all keys in one line, and a string representing the word to be typed. The program calculates the time it takes to type the word, where the time taken to move the finger from one key to another is the absolute difference between their indices. The program returns the total time taken to type the word by calling the function timeTaken().

Example

// C++ code to find the typing time for the word using a one row keyboard
#include <iostream>
#include <string>
using namespace std;

// To compute the time taken to type the word using a given single row keyboard, the following function is used.
int timeTaken(string keyboard_layout, string word){
   // Initialize time and previous position variables
   int time = 0, prev_pos = 0;
   // Iterate over each character in the word
   for (char c : word){
// Find the position of the current character in the keyboard_layout
      int curr_pos = keyboard_layout.find(c);
// Calculate the time taken to type the current character and add it to the total time
      time += abs(curr_pos - prev_pos);
      // Update the previous position to the current position
      prev_pos = curr_pos;
   }
   // Return the typing time for the word
   return time;
}
int main(){
   string keyboard_layout = "mnbvcxzqwertyuioplkjhgfdsa";
   string word = "tutorialspoint";
   // function call to calculate time taken to type word
   int time = timeTaken(keyboard_layout, word);
   // Print the result
   cout <<"Typing time for the word using a one row keyboard: " << time << endl;
   return 0;
}

Output

Typing time for the word using a one row keyboard: 87

Time and Space Complexity Analysis

Time Complexity: O(n)

  • n = length of the given word to be typed.

  • The program iterates through each character in the word string, which has a length of n.

  • For each character, it performs the find operation on the keyboard string, which has a length of 26 (a constant).

  • Therefore, the program’s time complexity = O(n * 26) = O(n).

Space Complexity: O(1)

  • The program uses a fixed amount of memory to store the keyboard and word strings, as well as a few integer variables.

  • This means that the program's memory requirements do not increase as the input size increases.

Conclusion

The article discusses an efficient solution approach to calculate time required to type a word using a given single-row keyboard. The problem statement is explained through examples for a better understanding. Furthermore, the solution approach consists of the pseudocode, dry run, C++ Program as well as a deep analysis of the time and space complexity of the solution for an in-depth understanding.

Updated on: 27-Aug-2023

58 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements