Description

Given a string keyboard of 26 unique lowercase English letters and a string word, return the total distance to type word if you only use one finger.

Examples

Input:keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba"
Output:4
Explanation:

c(2)+b(1)+a(1)=4.

Input:keyboard = "qwertyuiopasdfghjklzxcvbnm", word = "hello"
Output:54
Explanation:

Keyboard positions: h=15, e=2, l=18, o=8. Movement: 0->15 (15), 15->2 (13), 2->18 (16), 18->18 (0), 18->8 (10). Total = 15+13+16+0+10 = 54.

Input:keyboard = "abcdefghijklmnopqrstuvwxyz", word = "dad"
Output:9
Explanation:

Alphabetical keyboard: d=3, a=0. Movement: 0->3 (3), 3->0 (3), 0->3 (3). Total = 3+3+3 = 9.

Constraints

  • keyboard.length == 26

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!