C++: Basic Recursion + Memo (Commented)
\nclass Solution {\npublic:\n    int t[27][27][301];\n    int minimumDistance(string word) {\n        \n        memset(t, -1, sizeof(t));\n        \n        int
```\nclass Solution {\npublic:\n    int t[27][27][301];\n    int minimumDistance(string word) {\n        \n        memset(t, -1, sizeof(t));\n        \n        int ans = solve(word, -1, -1, 0);\n        \n        return ans;\n    }\n    \n    int solve(string& word, int fing1, int fing2, int k)\n    {\n        if(k >= word.size())\n            return 0;\n        \n        if(t[fing1 + 1][fing2 + 1][k] != -1) // displaced by 1 because we start with -1\n            return t[fing1 + 1][fing2 + 1][k];\n        \n        int interim = word[k] - \'A\'; // index of word[k] when started from A = 0\n        \n        pair <int, int> a = {interim / 6, interim % 6};  // row = interim / 6, column = interim % 6\n\t\t\n\t\t//temp1 and temp2 are distances which are equal to 0 when we start i.e. fing1  = -1 and fing2 = -1, else, we claculate the distance\n        \n        int temp1 = ((fing1 == -1) ? 0 : (abs(a.first - fing1 / 6) + abs(a.second - fing1 % 6)));\n        int temp2 = ((fing2 == -1) ? 0 : (abs(a.first - fing2 / 6) + abs(a.second - fing2 % 6)));\n        \n        int ans = temp1 + solve(word, interim, fing2, k + 1); //we choose finger 1 to point the particular word[k], \n//and when we do this, our finger1 is supposed to point at that word[k], hence insted of finger 1 we used interim.\n        \n        ans = min(ans, temp2 + solve(word, fing1, interim, k + 1)); // here we chose finger 2 and the same theory applies.\n        \n        return t[fing1 + 1][fing2 + 1][k] = ans;\n    }\n};\n\n/*\n    a / 6 - row\n    a % 6 - column\n*/\n```
['Recursion', 'Memoization']