Minimum Distance to Type a Word Using Two Fingers - Problem

Imagine you're a speed typist using a special QWERTY keyboard arranged in a grid layout. Each uppercase letter A-Z is positioned at specific coordinates on a 2D plane:

  • 'A' is at (0,0)
  • 'B' is at (0,1)
  • 'P' is at (2,3)
  • 'Z' is at (4,1)

Here's the twist: you can only use TWO fingers to type! Your goal is to type a given word with the minimum total distance traveled by your fingers.

Distance calculation: Manhattan distance between coordinates (x1, y1) and (x2, y2) is |x1 - x2| + |y1 - y2|

Special rules:

  • Your fingers can start at any position for free (no initial cost)
  • You don't have to start typing with the first letter immediately
  • At each step, choose which finger to move to the next letter

Return: The minimum total distance to type the entire word.

Input & Output

example_1.py — Basic word typing
$ Input: word = "CAKE"
Output: 3
💡 Note: One optimal strategy: Start finger1 at 'C' (free), finger2 at 'A' (free). Move finger1 from 'C' to 'K' (distance=2), move finger2 from 'A' to 'E' (distance=1). Total: 0 + 0 + 2 + 1 = 3
example_2.py — Single character
$ Input: word = "HAPPY"
Output: 6
💡 Note: Start both fingers free. Place finger1 at 'H' (cost=0), move to 'A' (cost=3), place finger2 at 'P' (cost=0), move to another 'P' (cost=0), move finger1 to 'Y' (cost=3). Total: 0+3+0+0+3=6
example_3.py — Edge case single letter
$ Input: word = "NEW"
Output: 3
💡 Note: Start finger1 at 'N' (free), finger2 at 'E' (free), then move finger1 from 'N' to 'W' (distance=3). Total distance is 3.

Constraints

  • 2 ≤ word.length ≤ 300
  • word consists of uppercase English letters only
  • Each letter A-Z is positioned at coordinate ((ord(c) - ord('A')) // 6, (ord(c) - ord('A')) % 6)
  • Initial finger positions are free (cost = 0)

Visualization

Tap to expand
QWERTY Grid LayoutABCDEFGHIFinger 1Finger 2Example: Typing 'AB'Step 1: Place fingers (cost = 0)• Finger 1 → A (0,0): cost = 0• Finger 2 → B (0,1): cost = 0Step 2: Type letters in order• Type 'A': Use Finger 1 (already there)• Type 'B': Use Finger 2 (already there)Total Distance: 0Distance Calculation FormulaManhattan Distance = |x₁ - x₂| + |y₁ - y₂|Example: Distance from A(0,0) to C(0,2)Distance = |0 - 0| + |0 - 2| = 0 + 2 = 2Example: Distance from B(0,1) to F(0,5)Distance = |0 - 0| + |1 - 5| = 0 + 4 = 4Key Strategy:At each step, choose the finger that minimizes total distance!
Understanding the Visualization
1
Setup Grid
Letters A-Z arranged in 6×5 grid, A at (0,0), Z at (4,1)
2
Initialize
Both fingers start free - can be placed anywhere at no cost
3
Choose Finger
For each letter, decide which finger to move based on minimum distance
4
Calculate Distance
Use Manhattan distance formula: |x1-x2| + |y1-y2|
Key Takeaway
🎯 Key Insight: Use dynamic programming to track optimal finger positions, taking advantage of free initial placement and making locally optimal decisions at each step.
Asked in
Google 45 Meta 32 Microsoft 28 Amazon 22
58.2K Views
Medium-High Frequency
~25 min Avg. Time
1.5K 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