C++ recursion with memo
\nclass Solution {\npublic:\n    \n    int dist(pair<int,int> x, pair<int,int> y){\n        return abs(x.first- y.first) + abs(x.second - y.second);\n    }\n   
```\nclass Solution {\npublic:\n    \n    int dist(pair<int,int> x, pair<int,int> y){\n        return abs(x.first- y.first) + abs(x.second - y.second);\n    }\n    \n    vector<vector<vector<int>>> cache;\n    \n    int helper(string& word, int curr, char x, char y, map<char, pair<int,int>>& data) {\n        if(word.size() == curr)\n            return 0;\n        if(cache[x - \'A\'][y - \'A\'][curr] != -1)\n            return cache[x - \'A\'][y - \'A\'][curr];\n        int a,b;\n        if(x == \'[\'){\n            a = helper(word, curr+1, word[curr], y, data);\n        }\n        else{\n            a = dist(data[word[curr]], data[x]) + helper(word, curr+1, word[curr], y, data);\n        }\n        if(y == \'[\') {\n            b = helper(word, curr+1, x, word[curr], data);\n        }\n        else{\n            b = dist(data[word[curr]], data[y]) + helper(word, curr+1, x, word[curr], data);\n        }\n        cache[x - \'A\'][y -\'A\'][curr] = min(a,b);\n        return cache[x - \'A\'][y -\'A\'][curr];\n    } \n    \n    int minimumDistance(string word) {\n        map<char, pair<int,int>> data;\n        int count = 0;\n        cache.resize(27, vector<vector<int>>(27, vector<int>(word.length(), -1)));\n        for(int i=0; i<5; i++) {\n            for(int j=0; j<6; j++) {\n                if(i == 4 && j ==2)\n                    break;\n                data[\'A\' + count] = make_pair(i,j);\n                count++;\n            }\n        }\n        return helper(word, 0, \'[\', \'[\', data);\n    }\n};\n```
['Recursion', 'Memoization']