Calculate time required to type a word using given single-row keyboard
Given a string keyboardLayout of size 26 representing the sequence of characters present in a single row of a keyboard and a string word, the task is to calculate the total time taken to type the word, starting from the 0th key, if moving to adjacent keys requires unit time.
Examples:
Input: keyboardLayout = “abcdefghijklmnopqrstuvwxyz”, word = “dog”
Output: 22
Explanation:
Pressing the key ‘d’ requires 3 units of time ( i.e. ‘a’ -> ‘b’ -> ‘c’ -> ‘d’)
Pressing the key ‘o’ requires 11 units of time ( i.e. ‘d’ -> ‘e’ -> ‘f’ -> ‘g’ -> ‘h’ -> ‘i’ -> ‘j’ -> ‘k’ -> ‘l’ -> ‘m’ -> ‘n’ -> ‘o’)
Pressing the key ‘g’ requires 8 units of time ( i.e. ‘o’ -> ‘n’ -> ‘m’ -> ‘l’ -> ‘k’ -> ‘j’ -> ‘i’ -> ‘h’ -> ‘g’)
Therefore, the total time taken = 3 + 11 + 8 = 22.
Input: keyboardLayout = “abcdefghijklmnopqrstuvwxyz”, word = “abcdefghijklmnopqrstuvwxyz”
Output: 25
Approach: Follow the steps below to solve the problem:
- Initialize a vector, say pos, to store the position of all the characters.
- Initialize two variables, say last, to store the last updated index, and result, to store the total time taken to type the word.
- Iterate over the characters of the string word:
- Initialize two variables, say destination, to store the index of the next character required to be typed, and distance, to store the distance of that index from the current index.
- Add the value of distance to result.
- Update the last to destination.
- After completing the above operations, print the value of result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int timeTakenToType(string& keyboardLayout,
string& word)
{
vector< int > pos(26);
for ( int i = 0; i < 26; ++i) {
char ch = keyboardLayout[i];
pos[ch - 'a' ] = i;
}
int last = 0;
int result = 0;
for ( int i = 0; i < ( int )word.size(); ++i) {
char ch = word[i];
int destination = pos[ch - 'a' ];
int distance = abs (destination - last);
result += distance;
last = destination;
}
cout << result;
}
int main()
{
string keyboardLayout
= "acdbefghijlkmnopqrtsuwvxyz" ;
string word = "dog" ;
timeTakenToType(keyboardLayout, word);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void timeTakenToType(String keyboardLayout,
String word)
{
int [] pos = new int [ 26 ];
for ( int i = 0 ; i < 26 ; i++) {
char ch = keyboardLayout.charAt(i);
pos[ch - 'a' ] = i;
}
int last = 0 ;
int result = 0 ;
for ( int i = 0 ; i < word.length(); i++) {
char ch = word.charAt(i);
int destination = pos[ch - 'a' ];
int distance = Math.abs(destination - last);
result += distance;
last = destination;
}
System.out.println(result);
}
public static void main(String[] args)
{
String keyboardLayout
= "acdbefghijlkmnopqrtsuwvxyz" ;
String word = "dog" ;
timeTakenToType(keyboardLayout, word);
}
}
|
Python3
def timeTakenToType(keyboardLayout, word):
pos = [ 0 ] * ( 26 )
for i in range ( 26 ):
ch = keyboardLayout[i]
pos[ ord (ch) - ord ( 'a' )] = i
last = 0
result = 0
for i in range ( len (word)):
ch = word[i]
destination = pos[ ord (ch) - ord ( 'a' )]
distance = abs (destination - last)
result + = distance
last = destination
print (result)
if __name__ = = '__main__' :
keyboardLayout = "acdbefghijlkmnopqrtsuwvxyz"
word = "dog"
timeTakenToType(keyboardLayout, word)
|
C#
using System;
public class GFG {
static void timeTakenToType(String keyboardLayout,
String word)
{
int [] pos = new int [26];
for ( int i = 0; i < 26; i++) {
char ch = keyboardLayout[i];
pos[ch - 'a' ] = i;
}
int last = 0;
int result = 0;
for ( int i = 0; i < word.Length; i++) {
char ch = word[i];
int destination = pos[ch - 'a' ];
int distance = Math.Abs(destination - last);
result += distance;
last = destination;
}
Console.WriteLine(result);
}
public static void Main(String[] args)
{
String keyboardLayout
= "acdbefghijlkmnopqrtsuwvxyz" ;
String word = "dog" ;
timeTakenToType(keyboardLayout, word);
}
}
|
Javascript
<script>
function timeTakenToType(keyboardLayout, word)
{
var pos = Array(26).fill(0);
for ( var i = 0; i < 26; ++i)
{
var ch = keyboardLayout[i];
pos[ch.charCodeAt(0) -
'a' .charCodeAt(0)] = i;
}
var last = 0;
var result = 0;
for ( var i = 0; i < word.length; ++i)
{
var ch = word[i];
var destination = pos[ch.charCodeAt(0) -
'a' .charCodeAt(0)];
var distance = Math.abs(destination - last);
result += distance;
last = destination;
}
document.write(result);
}
var keyboardLayout = "acdbefghijlkmnopqrtsuwvxyz" ;
var word = "dog" ;
timeTakenToType(keyboardLayout, word);
</script>
|
Time Complexity: O(N), where N is the size of the string word.
Auxiliary Space: O(1)
Last Updated :
13 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...