Imagine you have a special keyboard with all keys in a single row. The layout of characters on a keyboard is denoted by a string S1 of length 26. S1 is indexed from 0 to 25. Initially the index is at 0. To type a character we need to move the index of desired character. The time taken to move the index from i to j is |j-i| where || is absolute Value.
Given a string s1 and s2, describe the time taken to type string s2.
Given two string S1 and S2 where S1 = abcdeghijklmnopqrstuvwxyz
s2 = cba
and starting with index 0 ('A') find the time taken to type cba.
index of c = 2
count = 2 - 0
count = 2 + 1 (c to b )
count = 2 + 1(c to b) + 1 (b to a)
count returned should be 4.
I am sure this is easy to do in quadratic by running two loops but that is not an efficient solution. Any ideas how I can improve this?