0% found this document useful (0 votes)
18 views2 pages

Keyboard

Uploaded by

jagan chary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Keyboard

Uploaded by

jagan chary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

In KMIT institution there is a special keyboard designed by a student.

It contains only one row of 26-keys, keys[], each key indicates a unique letter
in the alphabet from 'a' to 'z' in the given order.

The order of keys in that keyboard will be given.


You need to findout the time taken to type a word.

The rules to calculate the time is as follows:


* Initially you will be at the first key .i.e, keys[0].
* To type a letter, you have to move to the key having desired letter, key[j].
* Time taken to type the character from key at i-th index to key at j-th index
is |i - j|.

NOTE: You need to press only one key at a time to type the word.

INPUT FORMAT:
-------------
Line-1: A string S, consists of order of the keys in the keyboard.
Line-2: A string W, the word to be typed.

OUTPUT FORMAT:
--------------
Print an integer result, the time taken to type the word.

SAMPLE INPUT-1:
---------------
abcdefghijklmnopqrstuvwxyz

SAMPLE OUTPUT-1:
----------------
code

EXPLANATION:
------------
Given word is "code"
- Time taken to type letter-c: from keys[0] to keys[2] => | 0 - 2 | = 2
- Time taken to type letter-o: from keys[2] to keys[14] => | 2 - 14 | = 12
- Time taken to type letter-d: from keys[14] to keys[3] => | 14 - 3 | = 11
- Time taken to type letter-e: from keys[3] to keys[4] => | 3 - 4 | = 1
Total time taken is : 26

SAMPLE INPUT-2:
---------------
poiuytrewqasdfghjklmnbvcxz
kmit

SAMPLE OUTPUT-2:
----------------
39

EXPLANATION:
------------
Given word is "kmit"
- Time taken to type letter-k: from keys[0] to keys[17] => | 0 - 17 | = 17
- Time taken to type letter-m: from keys[17] to keys[19] => | 17 - 19 | = 2
- Time taken to type letter-i: from keys[19] to keys[2] => | 20 - 2 | = 17
- Time taken to type letter-t: from keys[2] to keys[5] => | 2 - 5 | = 3
Total time taken is : 39

=== Testcases ===


case =1
input =poiuytrewqasdfghjklmnbvcxz
kmit
output =39

case =2
input =abcdefghijklmnopqrstuvwxyz
code
output =26

case =3
input =qazwsxedcrfvtgbyhnujmikolp
confirmation
output =117

case =4
input =qwertyuiopasdfghjklmnbvcxz
adobeconnect
output =154

case =5
input =mlpnkojibhuvgycftxdrzseawq
telescope
output =104

case =6
input =asdfghjklpoiuytrewqzxcvbnm
keshavmemorialinstituteoftechnology
output =289

case =7
input =mnbvcxzlkjhgfdsapoiuytrewq
tessellatorforngitandkmit
output =207

case =8
input =poiuytrewqzxcvbasdfghjnmkl
abcdefghijklmnopqrstuvwxyz
output =178

-----------------------------------------------------------

n=input()
x=input()
res=n.index(x[0])
for i in range(len(x)-1):
res=res+abs(n.index(x[i])-n.index(x[i+1]))
print(res)

You might also like