Tap here to switch tabs
Problem
Submissions

Problem: Total Distance to Type a String Using One Finger

med
30 min
Try to solve the Total Distance to Type a String Using One Finger problem.

Statement

There is a standard keyboard layout with 33 rows of lowercase English letters:

  • Row 00: "qwertyuiop"

  • Row 11: "asdfghjkl"

  • Row 22: "zxcvbnm"

Each letter is located at a coordinate (r,c)(r, c), where rr is the 00 indexed row number and cc is the 00 indexed column number within that row.

You start with your finger positioned at the letter 'a' at coordinate (1,0)(1, 0). To type a character, you move your finger from its current position (r1,c1)(r_1, c_1) to the target key’s position (r2,c2)(r_2, c_2). The distance of this movement is the Manhattan distance, defined as ∣r1−r2∣+∣c1−c2∣|r_1 - r_2| + |c_1 - c_2|.

Given a string s, return the total distance traveled to type all characters in s in sequence using one finger.

Constraints:

  • 1≤1 \leq s.length ≤104\leq 10^4

  • s consists of lowercase English letters only.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Total Distance to Type a String Using One Finger

med
30 min
Try to solve the Total Distance to Type a String Using One Finger problem.

Statement

There is a standard keyboard layout with 33 rows of lowercase English letters:

  • Row 00: "qwertyuiop"

  • Row 11: "asdfghjkl"

  • Row 22: "zxcvbnm"

Each letter is located at a coordinate (r,c)(r, c), where rr is the 00 indexed row number and cc is the 00 indexed column number within that row.

You start with your finger positioned at the letter 'a' at coordinate (1,0)(1, 0). To type a character, you move your finger from its current position (r1,c1)(r_1, c_1) to the target key’s position (r2,c2)(r_2, c_2). The distance of this movement is the Manhattan distance, defined as ∣r1−r2∣+∣c1−c2∣|r_1 - r_2| + |c_1 - c_2|.

Given a string s, return the total distance traveled to type all characters in s in sequence using one finger.

Constraints:

  • 1≤1 \leq s.length ≤104\leq 10^4

  • s consists of lowercase English letters only.