Tap here to switch tabs
Problem
Submissions

Problem: Minimum Distance to Type a Word Using Two Fingers

Try to solve the Minimum Distance to Type a Word Using Two Fingers problem.

Statement

Consider a keyboard where the 2626 uppercase English letters are arranged in a grid with 66 columns:

  • ′A′'A' is at (0,0)(0, 0), ′B′'B' is at (0,1)(0, 1), …, ′F′'F' is at (0,5)(0, 5)

  • ′G′'G' is at (1,0)(1, 0), ′H′'H' is at (1,1)(1, 1), …, ′L′'L' is at (1,5)(1, 5)

  • and so on, with ′Z′'Z' located at (4,1)(4, 1).

The distance between two coordinates (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is defined as ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

You are given a string word. You start with two fingers placed at any position on the keyboard at no initial cost. Moving a finger from its current letter position to a new letter position incurs a cost equal to the distance between the two letters.

Return the minimum total distance required to type all the characters in word in order.

Constraints:

  • 2≤2 \leq word.length ≤300\leq 300

  • word consists of uppercase English letters.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Distance to Type a Word Using Two Fingers

Try to solve the Minimum Distance to Type a Word Using Two Fingers problem.

Statement

Consider a keyboard where the 2626 uppercase English letters are arranged in a grid with 66 columns:

  • ′A′'A' is at (0,0)(0, 0), ′B′'B' is at (0,1)(0, 1), …, ′F′'F' is at (0,5)(0, 5)

  • ′G′'G' is at (1,0)(1, 0), ′H′'H' is at (1,1)(1, 1), …, ′L′'L' is at (1,5)(1, 5)

  • and so on, with ′Z′'Z' located at (4,1)(4, 1).

The distance between two coordinates (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is defined as ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

You are given a string word. You start with two fingers placed at any position on the keyboard at no initial cost. Moving a finger from its current letter position to a new letter position incurs a cost equal to the distance between the two letters.

Return the minimum total distance required to type all the characters in word in order.

Constraints:

  • 2≤2 \leq word.length ≤300\leq 300

  • word consists of uppercase English letters.