Posted on

Description

Submission

class Solution {
    int Map[26];
public:
    int calculateTime(string keyboard, string word) {
        for(int i = 0; i < keyboard.size(); ++i) {
            Map[keyboard[i]-'a'] = i;
        }
        int ret = 0;
        
        char prev = keyboard[0] - 'a';
        for(char c: word) {
            int cur = c - 'a';
            ret += abs(Map[cur]- Map[prev]);
            prev = cur;
        }
        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *