[C++] Recursion With Memoization
\nclass Solution {\npublic:\n    vector<vector<char>> mat;\n    int dp[300][7][7][7][7];\n    \n    int move(int x1, int y1, int x2, int y2) {\n        if(x1 ==
```\nclass Solution {\npublic:\n    vector<vector<char>> mat;\n    int dp[300][7][7][7][7];\n    \n    int move(int x1, int y1, int x2, int y2) {\n        if(x1 == -1 && y1 == -1)\n            return 0;\n        return abs(x2 - x1) + abs(y2 - y1);\n    }\n    \n    pair<int, int> pos(char c) {\n        for(int i = 0; i < mat.size(); i++) {\n            for(int j = 0; j < mat[0].size(); j++) {\n                if(mat[i][j] == c) {\n                    return { i, j };\n                } \n            }\n        }\n        return { -10, -10 };\n    }\n    \n    int f(string s, int i, int f1x, int f1y, int f2x, int f2y) {\n        if(i == s.size())\n            return 0;\n        if(dp[i][f1x + 1][f1y + 1][f2x + 1][f2y + 1] != -1)\n            return dp[i][f1x + 1][f1y + 1][f2x + 1][f2y + 1];\n        pair<int, int> p = pos(s[i]);\n        int cx = p.first, cy = p.second;\n        int op1 = move(f1x, f1y, cx, cy) + f(s, i + 1, cx, cy, f2x, f2y);\n        int op2 = move(f2x, f2y, cx, cy) + f(s, i + 1, f1x, f1y, cx, cy);\n        return dp[i][f1x + 1][f1y + 1][f2x + 1][f2y + 1] = min(op1, op2);\n    }\n    \n    int minimumDistance(string word) {\n        mat = {\n            {\n                \'A\', \'B\', \'C\', \'D\', \'E\', \'F\'\n            },\n            {\n                \'G\', \'H\', \'I\', \'J\', \'K\', \'L\'\n            },\n            {\n                \'M\', \'N\', \'O\', \'P\', \'Q\', \'R\'\n            },\n            {\n                \'S\', \'T\', \'U\', \'V\', \'W\', \'X\'\n            },\n            {\n                \'Y\', \'Z\'\n            },\n        };\n        memset(dp, -1, sizeof dp);\n        return f(word, 0, -1, -1, -1, -1);\n    }\n};\n```
[]