# HackerEarth Roy and Texting Robot problem solution

In this HackerEarth Roy and Texting Robot problem solution, Roy frequently needs to use his old Nokia cell phone for texting.

You may be already familiar with the working of the keypad, however, if you're not we shall see a few examples.

To type "b", we need to press "2" twice. To type "?" we need to press "1" thrice. To type "5" we need to press "5" four times. To type "0" we need to press "0" twice.

I hope that it's clear how the keypad is being used.

Now Roy has a lot of text messages and they are pretty lengthy. So he devised a mechanical hand robot (with only 1 finger) that does the typing job. One drawback of this robot is its typing speed. It takes 1 second for the single press of any button. Also, it takes 1 second to move from one key to another. The initial position of the hand robot is at key 1.

So if it's supposed to type "hack" it will take 1 second to move from key 1 to key 4 and then 2 seconds to type "h".
Now again 1 second to move from key 4 to key 2 and then 1 second to type "a".
Since "c" is present at the same key as "a" movement time is 0. So it simply takes 3 seconds to type "c".
Finally, it moves from key 2 to key 5 in 1 second and then 2 seconds to type "k". So in total it took 1+2+1+1+3+1+2= 11 seconds to type "hack".

## HackerEarth Roy and Texting Robot problem solution.

`key_map = {  ' ': 0,  '0': 0,  '.': 1,  ',': 1,  '?': 1,  '!': 1,  '1': 1,  'a': 2,  'b': 2,  'c': 2,  '2': 2,  'd': 3,  'e': 3,  'f': 3,  '3': 3,  'g': 4,  'h': 4,  'i': 4,  '4': 4,  'j': 5,  'k': 5,  'l': 5,  '5': 5,  'm': 6,  'n': 6,  'o': 6,  '6': 6,  'p': 7,  'q': 7,  'r': 7,  's': 7,  '7': 7,  't': 8,  'u': 8,  'v': 8,  '8': 8,  'w': 9,  'x': 9,  'y': 9,  'z': 9,  '9': 9,}count_map = {  ' ': 1,  '0': 2,  '.': 1,  ',': 2,  '?': 3,  '!': 4,  '1': 5,  'a': 1,  'b': 2,  'c': 3,  '2': 4,  'd': 1,  'e': 2,  'f': 3,  '3': 4,  'g': 1,  'h': 2,  'i': 3,  '4': 4,  'j': 1,  'k': 2,  'l': 3,  '5': 4,  'm': 1,  'n': 2,  'o': 3,  '6': 4,  'p': 1,  'q': 2,  'r': 3,  's': 4,  '7': 5,  't': 1,  'u': 2,  'v': 3,  '8': 4,  'w': 1,  'x': 2,  'y': 3,  'z': 4,  '9': 5,}def texting_robot():  t = input()  for tt in xrange(t):    s = raw_input()    res = 0    slen = len(s)    i = 0    curr = 1    while i < slen:      if curr != key_map[s[i]]:        res += 1      res += count_map[s[i]]      curr = key_map[s[i]]      i += 1    print restexting_robot()`

### Second solution

`#include <cstdio>#include <string>#include <iostream>#include <cassert>using namespace std;#define MAXT 50#define MAXL 1000string keys = {"_0", ".,?!1", "abc2", "def3", "ghi4", "jkl5", "mno6", "pqrs7", "tuv8", "wxyz9"};bool valid(char s){    if (s=='.' or s==',' or s=='?' or s=='!' or s=='_')        return true;    if (s>='a' and s<='z')        return true;    if (s>='0' and s<='9')        return true;    return false;}int getPos(char c){    for(int i=0;i<10;i++){        for (int j=0;j<keys[i].size();j++)            if(keys[i][j]==c)                return i;    }}int press(char c, int p){    for(int i=0;i<keys[p].size();i++)        if(keys[p][i]==c)            return i+1;}int main(){    int T, len, ans, pos, newpos;    string S;    scanf("%d", &T);    assert(T>0 and T<=MAXT);    while(T--){        cin >> S;        len = S.size();        assert(len>0 and len<=MAXL);        ans = 0;        pos = 1;        for(int i=0;i<len;i++){            assert(valid(S[i]));            newpos = getPos(S[i]);            if(newpos!=pos)                ans++;            pos = newpos;            ans = ans + press(S[i], pos);        }        printf("%d\n", ans);    }}`