C++ Solution
\nclass Solution {\npublic:\n    unordered_map<char,unordered_map<char,int>>dist;\n    unordered_map<char,pair<int,int>>pos;\n    int dp[350][27][27];\n    int 
```\nclass Solution {\npublic:\n    unordered_map<char,unordered_map<char,int>>dist;\n    unordered_map<char,pair<int,int>>pos;\n    int dp[350][27][27];\n    int recur(int ind,char c1,char c2,string &word)\n    {\n        if(ind==word.length())\n            return 0;\n        if(dp[ind][c1-\'A\'][c2-\'A\']!=-1)\n            return dp[ind][c1-\'A\'][c2-\'A\'];\n        return dp[ind][c1-\'A\'][c2-\'A\']=min(dist[c1][word[ind]]+recur(ind+1,word[ind],c2,word),dist[c2][word[ind]]+recur(ind+1,c1,word[ind],word));\n    }\n        \n    int get_ans(char c1,char c2,string &word)\n    {\n        return recur(0,c1,c2,word);\n    }\n    int minimumDistance(string word) {\n       \n        int myans=100000;\n        memset(dp,-1,sizeof(dp));\n        char c=\'A\';\n\t\t\n\t\t// pos of each character in matrix given used to calculate distance between them\n        for(int i=0;i<5;i++)\n        {\n            for(int j=0;j<6;j++)\n            {\n                pos[c++]={i,j};\n            }\n        }\n\t\t\n\t\t//dist between each character\n        for(char c1=\'A\';c1<=\'Z\';c1++)\n        {\n            for(char c2=\'A\';c2<=\'Z\';c2++)\n            {\n                dist[c1][c2]=abs(pos[c1].first-pos[c2].first)+abs(pos[c1].second-pos[c2].second);\n            }\n        }\n\t\t\n\t\t// taking 2 characters and simple Recursive DP after that\n        for(char c1=\'A\';c1<=\'Z\';c1++)\n        {\n            for(char c2=\'A\';c2<=\'Z\';c2++)\n            {\n                if(c1!=c2)\n                {\n                    myans=min(myans,get_ans(c1,c2,word));\n                }\n            }\n        }\n        return myans;\n    }\n};\n```
[]