C++ DP | RECURSION | MEMOIZATION
\nclass Solution {\npublic:\n    int dp[301][270][270];\n    int helper(string &s, int i,char pre1, char pre2, unordered_map<char,pair<int,int>> &h){\n        i
```\nclass Solution {\npublic:\n    int dp[301][270][270];\n    int helper(string &s, int i,char pre1, char pre2, unordered_map<char,pair<int,int>> &h){\n        if(i==s.length()){\n            \n            return 0;\n        }\n       if(dp[i][pre1][pre2]!=-1){\n           return dp[i][pre1][pre2];\n       }\n        pair<int,int> p1 = h[pre1];\n        pair<int,int> finals = h[s[i]];\n        int cost = abs(p1.first - finals.first) + abs(p1.second - finals.second) + helper(s,i+1,s[i],pre2,h);\n        if(pre2==\'0\'){\n            cost = min(cost,helper(s,i+1,pre1,s[i],h));\n        }\n        else{\n                pair<int,int> p2 = h[pre2];\n            int temp =  abs(p2.first -finals.first) + abs(p2.second - finals.second);\n            cost = min(cost,temp + helper(s,i+1,pre1,s[i],h));\n                \n        }\n        return dp[i][pre1][pre2] = cost;\n    }\n    \n    int minimumDistance(string word) {\n        unordered_map<char,pair<int,int>> h;\n        for(int i= 0; i < 26;i++){\n            char c = i+\'A\';\n            h[c] = {i/6,i%6};\n        }\n        memset(dp,-1,sizeof(dp));\n        return helper(word,1,word[0],\'0\',h);\n    }\n};\n```
['Recursion', 'Memoization', 'C']