Single-Row Keyboard - Problem

Imagine a futuristic keyboard where all 26 letters are arranged in a single horizontal row instead of the traditional QWERTY layout. You're a one-finger typist (like many of us on mobile phones!) and need to calculate the total time required to type a word.

Given:

  • A string keyboard of length 26 representing the custom layout (each letter appears exactly once)
  • A string word that you want to type

Rules:

  • Your finger starts at position 0 (the first key)
  • To type any character, you must move your finger to that character's position
  • The time to move from position i to position j is |i - j|

Goal: Return the minimum time needed to type the entire word.

Example: If keyboard = "abcdefghijklmnopqrstuvwxyz" and word = "cba", you'd move: 0→2 (cost 2), 2→1 (cost 1), 1→0 (cost 1) = total time of 4.

Input & Output

example_1.py — Basic Example
$ Input: keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba"
Output: 4
💡 Note: Starting at position 0: move to 'c' at position 2 (cost 2), then to 'b' at position 1 (cost 1), then to 'a' at position 0 (cost 1). Total: 2+1+1 = 4.
example_2.py — Custom Keyboard Layout
$ Input: keyboard = "pqrstuvwxyzabcdefghijklmno", word = "leetcode"
Output: 73
💡 Note: With this custom layout, characters are in different positions. We calculate the distance for each character move: l→e→e→t→c→o→d→e, summing all the absolute differences between consecutive positions.
example_3.py — Single Character
$ Input: keyboard = "abcdefghijklmnopqrstuvwxyz", word = "z"
Output: 25
💡 Note: Edge case with single character. Starting at position 0, we need to move to 'z' at position 25, so the total time is |0-25| = 25.

Constraints

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

Visualization

Tap to expand
a b c d e f g h i j k l m n o p q r s t u v w x y z👆Hash Table Lookup Process:Word: "cba"1. 'c' → position 2 (move 0→2, cost: 2)2. 'b' → position 1 (move 2→1, cost: 1)3. 'a' → position 0 (move 1→0, cost: 1)⚡ Efficiency BoostBrute Force: O(26) per characterHash Table: O(1) per characterTotal Time: 4 units🎯 Key Insight: Build the position map once, then lookup instantly!Time: O(n) | Space: O(1)
Understanding the Visualization
1
Initialize
Place finger at starting position (index 0) on the keyboard
2
Lookup Position
Use hash table to instantly find the target character's position
3
Calculate Distance
Compute absolute difference between current and target positions
4
Move Finger
Add the distance to total time and update current position
5
Repeat
Continue for each character in the word
Key Takeaway
🎯 Key Insight: Instead of searching through the keyboard repeatedly, create a hash table once to enable instant O(1) character position lookups, reducing time complexity from O(n×26) to O(n).
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
23.4K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen