Least Two-Finger Travel on the Six-Column Keyboard

Quick Overview

Least Two-Finger Travel on the Six-Column Keyboard. The keyboard rows are ABCDEF, GHIJKL, MNOPQR, STUVWX, YZ.

Least Two-Finger Travel on the Six-Column Keyboard

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem The keyboard rows are `ABCDEF`, `GHIJKL`, `MNOPQR`, `STUVWX`, `YZ`. Letter index t from A=0 has coordinates(t/6,t%6). Type word in order using either of two fingers for each letter. A movement costs Manhattan distance between key coordinates. Each finger's initial position is freely chosen and incurs no cost. Return the minimum total travel. ```cpp class Solution { public: int minimumDistance(string word); }; ``` ### Examples **Example 1:** ```text Input: word = "AB" Output: 0 Explanation: Place one finger on A and the other on B initially. ``` **Example 2:** ```text Input: word = "ABC" Output: 1 Explanation: One finger types A then moves one unit to B; the other starts on C. Three distinct keys require at least one positive movement. ``` ### Constraints - word has length 2 to 300. - Every character is an uppercase English letter. ### Contract considerations An unplaced finger may choose its first key without movement cost. Both fingers may begin at any keys; only subsequent movement contributes. ```hint Read the boundary condition Check repeated letters, the short final keyboard row, and the freely chosen initial locations of both fingers. ``` ### Boundary reminders An unplaced finger may choose its first key without movement cost. Both fingers may begin at any keys; only subsequent movement contributes.

Overview: Least Two-Finger Travel on the Six-Column Keyboard. The keyboard rows are ABCDEF, GHIJKL, MNOPQR, STUVWX, YZ.

Google logo
Google
Jun 12, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The keyboard rows are ABCDEF, GHIJKL, MNOPQR, STUVWX, YZ. Letter index t from A=0 has coordinates(t/6,t%6). Type word in order using either of two fingers for each letter. A movement costs Manhattan distance between key coordinates. Each finger's initial position is freely chosen and incurs no cost. Return the minimum total travel.

class Solution {
public:
    int minimumDistance(string word);
};

Examples

Example 1:

Input: word = "AB"
Output: 0
Explanation: Place one finger on A and the other on B initially.

Example 2:

Input: word = "ABC"
Output: 1
Explanation: One finger types A then moves one unit to B; the other starts on C. Three distinct keys require at least one positive movement.

Constraints

  • word has length 2 to 300.
  • Every character is an uppercase English letter.

Contract considerations

An unplaced finger may choose its first key without movement cost. Both fingers may begin at any keys; only subsequent movement contributes.

Boundary reminders

An unplaced finger may choose its first key without movement cost. Both fingers may begin at any keys; only subsequent movement contributes.