1165. Single-Row Keyboard
Problem Description
In this problem, we're given a string keyboard
which represents the layout of a special keyboard with all the keys in a single row. The layout is a string of length 26
, containing each letter of the English alphabet exactly once. The goal is to type a given string word
using this keyboard, knowing that our finger starts at the position of the 'a' character or index 0
.
We need to calculate the total time it takes to type the entire word, where the "time" is defined as the number of steps our finger moves from one character to the next. The time to move from one character at index i
to another at index j
is given by the absolute difference in their positions, |i - j|
. The function we write must return the sum of these movements as we type out the word character by character.
Intuition
To solve this problem, we identify that the key operation is to calculate the distance between consecutive characters in the word
. We need a fast way to look up the position of each character on the keyboard
. A suitable data structure for this kind of look-up is a map or dictionary, where each key is a character from the keyboard and the corresponding value is its index.
Here's the step-by-step reasoning to create our solution:
-
Create a dictionary that maps each character (key) to its respective index (value) on the keyboard. This will give us O(1) access time to any character's index when we're typing the word.
-
Initialize a variable to keep track of the total time (or steps) spent moving between characters.
-
Start from the beginning position, index
0
, and iterate over each character in the givenword
. -
For each character in the word, calculate the distance from the current index to the character's index using the absolute difference between indices.
-
Add the distance to the total time and update the current index to the character's index.
-
Return the total time after iterating through all characters in
word
.
This approach is straightforward and efficient, virtually simulating the typing process for each character in the word and accumulating the total movement cost.
Solution Approach
The solution uses a dictionary to store the keyboard layout mapping, efficient iteration over the input string, and simple arithmetic to calculate the total time taken to type the word. Here's how the implementation unfolds:
-
A dictionary (hash map) is created with a comprehension
pos = {c: i for i, c in enumerate(keyboard)}
. This maps each characterc
on the keyboard to its indexi
. Theenumerate()
function provides a convenient way of getting both the character and its index. -
We use two variables in our solution:
ans
to track the total time taken, andi
to keep track of the index of our finger's current position on the keyboard. Initially,ans
is set to0
andi
to0
, as our starting position is at the index0
. -
The solution then iterates over each character
c
in theword
using a for loop:for c in word:
. For each iteration, it performs two main operations:- First, it calculates the absolute difference between the current finger position and the target character’s position using
abs(pos[c] - i)
. This gives us the distance or the number of steps needed to move the finger from the current position to the position of the characterc
on the keyboard. - Second, it adds this distance to
ans
, which accumulates the total time taken so far. After adding the distance, it updates the current positioni
to the position of the characterc
, i.e.,i = pos[c]
.
- First, it calculates the absolute difference between the current finger position and the target character’s position using
-
After iterating through all characters, the total time
ans
is returned, which gives us the answer to the problem.
There are no complex algorithms used in this solution; it primarily hinges on the efficient access of elements in a dictionary, and the computation of absolute differences between integers, which is constant-time operation. This solution is particularly efficient because each letter in word
is looked up exactly once, resulting in O(n) time complexity, where n is the length of the word
. The space complexity is O(1) since the dictionary holds a constant 26 key-value pairs, corresponding to the number of letters in the English alphabet.
Ready to land your dream job?
Unlock your dream job with a 2-minute evaluator for a personalized learning plan!
Start EvaluatorExample Walkthrough
Let's go through the solution approach with a small example to illustrate how it works in action. Assume we have the following inputs:
keyboard = "pqrstuvwxyzabcdefghijklmno"
word = "code"
According to the problem, a dictionary is to be created first that will map each character of the keyboard
to its index. Let's create this dictionary:
keyboard = "pqrstuvwxyzabcdefghijklmno"
# The dictionary mapping each character to its index.
pos = {c: i for i, c in enumerate(keyboard)}
# The dictionary will look something like this:
# {'p': 0, 'q': 1, 'r': 2, ..., 'n': 24, 'o': 25}
Now we need to type out the word
"code". We start at index 0
, which is the position of the 'a' character according to the problem statement. But since our keyboard
starts with 'p', 'a' is at index 15
in our mapping. Initially, both the ans
variable (total time) and i
variable (current position) will be set to 0
.
Let's simulate the typing:
- To type "c", our finger moves from index
0
to index16
. Soans += abs(pos['c'] - i)
. This isans += abs(16 - 0)
, which is16
.- Update
i
to16
(position of 'c').
- Update
- Next is "o", with
i
at16
and 'o' at index25
, soans += abs(25 - 16)
, which is9
.- Update
i
to25
.
- Update
- Then "d", with
i
at25
and 'd' is at index13
, soans += abs(13 - 25)
, which is12
.- Update
i
to13
.
- Update
- Lastly, we type "e", with
i
at13
and 'e' at index14
, soans += abs(14 - 13)
, which is1
.- Update
i
to14
.
- Update
Adding up all the movements, ans = 16 + 9 + 12 + 1
, which equals 38
. Therefore, the total time taken to type the word "code" on this special keyboard would be 38
steps.
The entire walk-through we just did represents exactly what the algorithm does behind the scenes, using efficient mappings and arithmetic operations to find the solution.
Solution Implementation
1class Solution:
2 def calculate_time(self, keyboard: str, word: str) -> int:
3 # Create a dictionary to map each character to its index in the keyboard.
4 char_to_index = {char: index for index, char in enumerate(keyboard)}
5
6 # Initialize the total time and the starting index.
7 total_time = current_index = 0
8
9 # Iterate through each character in the word.
10 for char in word:
11 # Calculate the time to move from the current_index to the index of the char.
12 total_time += abs(char_to_index[char] - current_index)
13 # Update the current index to the index of the char.
14 current_index = char_to_index[char]
15
16 # Return the total time to type the word.
17 return total_time
18
1class Solution {
2 public int calculateTime(String keyboard, String word) {
3 // Create an array to store the index positions of each character in the keyboard
4 int[] charPositions = new int[26];
5 // Fill the array with the index positions
6 for (int i = 0; i < 26; ++i) {
7 charPositions[keyboard.charAt(i) - 'a'] = i;
8 }
9
10 // Initialize the total time taken to type the word to 0
11 int totalTime = 0;
12 // Set the initial position to the start of the keyboard (index 0)
13 int currentPosition = 0;
14
15 // Iterate over each character in the word
16 for (int k = 0; k < word.length(); ++k) {
17 // Find the index position of the current character
18 int targetPosition = charPositions[word.charAt(k) - 'a'];
19 // Add the distance to travel from the current position to the target position
20 totalTime += Math.abs(currentPosition - targetPosition);
21 // Update the current position to be the target position for the next iteration
22 currentPosition = targetPosition;
23 }
24 // Return the total time taken to type the word
25 return totalTime;
26 }
27}
28
1class Solution {
2public:
3 int calculateTime(string keyboard, string word) {
4 // Create an array to hold the indices of each character in the keyboard.
5 int charPositions[26];
6
7 // Fill the array with each character's index from the 'keyboard' string.
8 for (int i = 0; i < 26; ++i) {
9 charPositions[keyboard[i] - 'a'] = i; // 'a' maps to 0, 'b' maps to 1, etc.
10 }
11
12 // Initialize 'totalTime' to record the total time to type the word.
13 int totalTime = 0;
14 // Initialize 'currentPosition' to track the current position of the finger on the keyboard, starting at index 0.
15 int currentPosition = 0;
16
17 // Loop over each character in the 'word' string to calculate the total time.
18 for (char& currentChar : word) {
19 // Determine the next position of the character on the keyboard.
20 int nextPosition = charPositions[currentChar - 'a'];
21 // Add the distance from the current position to the next position to 'totalTime'.
22 totalTime += abs(currentPosition - nextPosition);
23 // Update 'currentPosition' to be the next position for the following iteration.
24 currentPosition = nextPosition;
25 }
26
27 // Return the calculated total time.
28 return totalTime;
29 }
30};
31
1function calculateTime(keyboard: string, word: string): number {
2 // Create an array to store the position of each character in the keyboard.
3 const keyPositions: number[] = new Array(26).fill(0);
4
5 // Fill the keyPositions array with the correct positions of the characters.
6 for (let index = 0; index < keyboard.length; ++index) {
7 // Calculate the position based on character 'a' having charCode of 97.
8 keyPositions[keyboard.charCodeAt(index) - 'a'.charCodeAt(0)] = index;
9 }
10
11 // Initialize the total time to 0.
12 let totalTime = 0;
13
14 // Initialize the current position to the starting point (0).
15 let currentPosition = 0;
16
17 // Iterate through each character in the word.
18 for (const char of word) {
19 // Find the target position for the current character.
20 const targetPosition = keyPositions[char.charCodeAt(0) - 'a'.charCodeAt(0)];
21
22 // Add to the total time the distance from the current position to the target position.
23 totalTime += Math.abs(currentPosition - targetPosition);
24
25 // Move the current position to the target position.
26 currentPosition = targetPosition;
27 }
28
29 // Return the total time to type out the word.
30 return totalTime;
31}
32
Time and Space Complexity
Time Complexity
The algorithm consists of two parts: building a dictionary with positions of characters in the keyboard
, and then iterating over the word
to calculate the total time.
-
Building the position dictionary has a time complexity of
O(n)
, wheren
is the length of thekeyboard
string. This is because each character in thekeyboard
string is visited once. -
Calculating the time takes
O(m)
, wherem
is the length of theword
. Each character in theword
requires a constant time operation of addition and obtaining the value from the dictionary, which is in O(1).
Therefore, the overall time complexity of the calculateTime
function is O(n + m)
.
Space Complexity
The space complexity of the algorithm is also determined by two factors:
-
The position dictionary, which contains as many entries as there are characters in
keyboard
. This results in a space complexity ofO(n)
. -
The variables
ans
andi
use constant space, so they do not scale with the input size.
Hence, the total space complexity is O(n)
due to the dictionary storing the positions of keyboard characters.
Learn more about how to find time and space complexity quickly using problem constraints.
Which type of traversal does breadth first search do?
Recommended Readings
LeetCode Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way we
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Want a Structured Path to Master System Design Too? Don’t Miss This!