1165. Single-Row Keyboard
Problem
Given a keyboard mapping and a word, return the total time to type the word, starting from index 0.
There is a special single-row keyboard where all keys are arranged in a single row. Given a string keyboard that represents the layout of the keyboard (26 lowercase letters), and a string word to type, return the total time required to type the word.
You start at position 0 (the first character of the keyboard). To type a character, you move from your current position to the position of that character on the keyboard. The time taken is the absolute distance between positions.
Examples
Input: ["abcdefghijklmnopqrstuvwxyz","cba"]
Output: 4
Input: ["pqrstuvwxyzabcdefghijklmno","leetcode"]
Output: 73
Hints
Precompute the position of each character in the keyboard using a map.
Keep track of the current position and add the absolute difference for each character.
The time to type the first character is simply its position on the keyboard.
Related Problems
1165. Single-Row Keyboard
Given a keyboard mapping and a word, return the total time to type the word, starting from index 0.