Linear Keyboard (Codeforces 1607A)
Tags: Implement, strings

You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$.

Input: The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.The next $$$2t$$$ lines contain descriptions of the test cases.The first line of a description contains a keyboard — a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard.The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters.

Output: Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard.


Sample Inputs:
5
abcdefghijklmnopqrstuvwxyz
hello
abcdefghijklmnopqrstuvwxyz
i
abcdefghijklmnopqrstuvwxyz
codeforces
qwertyuiopasdfghjklzxcvbnm
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qwertyuiopasdfghjklzxcvbnm
abacaba

Sample Outputs:
13
0
68
0
74

Solution (Python):
def typing_time(keyboard, word):
    position = {char: i for (i, char) in enumerate(keyboard)}
    total_time = 0
    previous_position = position[word[0]]
    for char in word[1:]:
        current_position = position[char]
        total_time += abs(previous_position - current_position)
        previous_position = current_position
    return total_time
t = int(input())
for _ in range(t):
    keyboard = input()
    word = input()
    print(typing_time(keyboard, word))

Solution (Python):
def calculate_typing_time(keyboard, word):
    position_map = {char: idx for (idx, char) in enumerate(keyboard)}
    time = 0
    previous_position = None
    for char in word:
        current_position = position_map[char]
        if previous_position is not None:
            time += abs(current_position - previous_position)
        previous_position = current_position
    return time

def main():
    t = int(input().strip())
    test_cases = []
    for _ in range(t):
        keyboard = input().strip()
        word = input().strip()
        test_cases.append((keyboard, word))
    for (keyboard, word) in test_cases:
        print(calculate_typing_time(keyboard, word))
if __name__ == '__main__':
    main()

Solution (Python):
def min_time_to_type(keyboard, word):
    time = 0
    prev_pos = keyboard.index(word[0])
    for i in range(1, len(word)):
        current_pos = keyboard.index(word[i])
        time += abs(current_pos - prev_pos)
        prev_pos = current_pos
    return time

def main():
    t = int(input().strip())
    for _ in range(t):
        keyboard = input().strip()
        word = input().strip()
        print(min_time_to_type(keyboard, word))
if __name__ == '__main__':
    main()