[C++] Simple DP!!
Take both fingers - use first finger and use second finger, give the one which gives minimum moves. To avoid TLE! memoize the answers\n\n\n\nclass Solution {\np
Take both fingers - use first finger and use second finger, give the one which gives minimum moves. To avoid TLE! memoize the answers\n\n\n```\nclass Solution {\npublic:\n    vector<string> v;\n    unordered_map<char, pair<int, int>> mp;\n    unordered_map<int, unordered_map<int, unordered_map<int, int>>> dp;\n    int f(int i, pair<int, int> fi, pair<int, int> f2, string &word)\n    {\n        if(i >= word.size())\n            return 0;\n        int first = -1;\n        if(fi.first != -1)\n            first = fi.first*(v[0].size()) + fi.second;\n        int second = -1;\n        if(f2.first != -1)\n            second = f2.first*(v[0].size()) + f2.second;\n        // memoize the solution to avoid TLE!!\n        if(dp.find(i) != dp.end() && dp[i].find(first) != dp[i].end() && dp[i][first].find(second) != dp[i][first].end())\n            return dp[i][first][second];\n        \n        int a = INT_MAX, b = INT_MAX;\n        pair<int, int> pos = mp[ word[i] ];\n        int x = pos.first, y = pos.second;\n        if(fi.first == -1)\n        {\n            a = f(i + 1, {x, y}, f2, word);\n        }\n        else\n        {\n            int dis = abs(fi.first - x) + abs(fi.second - y);\n            a = dis + f(i + 1, {x, y}, f2, word);\n        }\n        \n        if(f2.first == -1)\n        {\n            b = f(i + 1, fi, {x, y}, word);\n        }\n        else\n        {\n            int dis = abs(f2.first - x) + abs(f2.second - y);\n            b = dis + f(i + 1, fi, {x, y}, word);\n        }\n        dp[i][first][second] = min(a, b);\n        return min(a, b);\n    }\n    int minimumDistance(string word) {\n        v = vector<string> {"ABCDEF","GHIJKL","MNOPQR","STUVWX","YZ"};\n        mp.clear();\n        dp.clear();\n        for(int i = 0; i < v.size(); i++)\n        {\n            for(int j = 0; j < v[i].size(); j++)\n                mp[v[i][j]] = {i, j};\n        }\n        \n        return f(0, {-1,-1}, {-1,-1}, word);\n    }\n};\n```
[]