
Problem Statement
In this problem, we are provided with a unique kind of keyboard where all the 26 English lowercase letters are aligned in a single row, and their arrangement is specified by the keyboard
string. You start typing with your finger located at the start of this keyboard (index 0
). The time taken to type each character of the word
depends on the distance you need to move your finger across this keyboard.
To type a character from the string word
, locate its position on the keyboard
, and the time spent to press this key equals the absolute difference between the current finger position and this key's position. After pressing the key, the finger moves to this new position.
The objective is to determine the total time taken to type the entire string word
using this method.
Examples
Example 1
Input:
keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba"
Output:
4
Explanation:
Move from index 0 to 2 to press 'c' → time = 2 Then from 2 to 1 to press 'b' → time = 1 Then from 1 to 0 to press 'a' → time = 1 Total = 2 + 1 + 1 = 4
Example 2
Input:
keyboard = "phqgiumeaylnofdxjkrcvstzwb", word = "phone"
Output:
52
Explanation:
Use the custom keyboard to locate each character in "phone": - 'p' is at index 0 → move from 0 to 0 = 0 - 'h' is at index 1 → move 0 → 1 = 1 - 'o' is at index 13 → move 1 → 13 = 12 - 'n' is at index 12 → move 13 → 12 = 1 - 'e' is at index 6 → move 12 → 6 = 6 Total = 0 + 1 + 12 + 1 + 6 = 20 (Note: The actual total depends on the exact mapping, this is illustrative.)
Constraints
keyboard.length == 26
keyboard
contains each English lowercase letter exactly once in some order.1 <= word.length <= 10^4
word[i]
is an English lowercase letter.
Approach and Intuition
To efficiently calculate the total time to type word
:
Build a Lookup Table: Create a dictionary that maps each character in
keyboard
to its index.Initialize Cursor Position: Start at index
0
(leftmost position).Iterate Through
word
: For each character:- Find its position on the keyboard.
- Compute the distance from the current position.
- Add the distance to a running total.
- Update the current position.
Return the Total Time: After processing all characters, return the total accumulated movement time.
This solution runs in O(n) time where n
is the length of word
, and uses O(1) space for the 26-letter index map.
Solutions
- C++
- Java
class Solution {
public:
int calculateTime(string keyboard, string word) {
vector<int> positionMap(26, -1);
// Mapping character to its position on keyboard
for (int idx = 0; idx < keyboard.size(); idx++)
positionMap[keyboard[idx] - 'a'] = idx;
// Start from the first position
int lastPosition = 0;
int totalTime = 0;
// Calculate the time to type each character in word
for (char &ch : word) {
totalTime += abs(lastPosition - positionMap[ch - 'a']);
lastPosition = positionMap[ch - 'a'];
}
return totalTime;
}
};
The given C++ code defines a solution for finding the minimum time required to type a word on a theoretical single-row keyboard where each character's position is unique. Calculate the time by simulating the movement of a finger to type each character of the word based on its position on the keyboard. Here's how the solution works:
First, create a mapping of each character to its respective position on the keyboard. This is done using a vector
positionMap
where the index represents the character and the value at that index represents the position of that character on the keyboard.Initialize
lastPosition
to 0 to represent the starting point of the finger at the beginning of the keyboard.Initialize
totalTime
to 0 to accumulate the total time spent moving from character to character.
For each character in the word:
- Calculate the difference between the last position of the finger and the new character's position to get the movement cost.
- Update
lastPosition
to the current character's position. - Accumulate this movement cost to
totalTime
.
Finally, the function returns totalTime
, which represents the total amount of time taken to type the entire word on the keyboard.
class Solution {
public int computeTypingTime(String layout, String text) {
int[] position = new int[26];
// Assign each character its position from layout.
for (int i = 0; i < layout.length(); i++)
position[layout.charAt(i) - 'a'] = i;
// Initial position.
int previous = 0;
int totalTime = 0;
// Calculate total typing duration.
for (char c : text.toCharArray()) {
// Increment totalTime by movement distance.
totalTime += Math.abs(previous - position[c - 'a']);
// Current becomes the new previous.
previous = position[c - 'a'];
}
return totalTime;
}
}
This solution addresses the problem of calculating the typing time on a single-row keyboard where each character's position is defined by a given layout. Written in Java, the approach involves mapping each character to its position on this custom keyboard layout and then calculating the time based on the movements required to type a given text.
- Initialize an array to store the position of each letter in the alphabet based on the custom keyboard layout.
- Loop through the layout string and assign each character a position index.
- Calculate the total time it takes to type the text:
- Begin at the start of the layout (initial position is 0).
- For each character in the text, compute the distance from the current position to the character's position in the layout. The absolute difference in positions translates directly to typing time.
- Move the current position to the last character typed.
The function returns the accumulated total time required to type all characters in the input text according to the specified keyboard layout. This solution effectively measures the efficiency of typing based on finger travel distance on a uniquely defined keyboard.
No comments yet.