C++ || Beginner Friendly || Recursion || Memoization || 3D DP
Code (Recursion)\n\nclass Solution {\nprivate:\n    int recursion(int index, int fingerOne, int fingerTwo, int &size, string &word)\n    {\n        if(index >= 
# Code (Recursion)\n```\nclass Solution {\nprivate:\n    int recursion(int index, int fingerOne, int fingerTwo, int &size, string &word)\n    {\n        if(index >= size)\n        {\n            return 0;\n        }\n        int firstFinger = 0;\n        if(fingerOne == -1)\n        {\n            firstFinger = recursion(index + 1, word[index] - \'A\', fingerTwo, size, word);\n        }\n        else\n        {\n            int prevRow = fingerOne / 6;\n            int prevCol = fingerOne % 6;\n            int currRow = (word[index] - \'A\') / 6;\n            int currCol = (word[index] - \'A\') % 6;\n            int distance = abs(prevRow - currRow) + abs(prevCol - currCol);\n            firstFinger = distance + recursion(index + 1, word[index] - \'A\', fingerTwo, size, word);\n        }\n        int secondFinger = 0;\n        if(fingerTwo == -1)\n        {\n            secondFinger = recursion(index + 1, fingerOne, word[index] - \'A\', size, word);\n        }\n        else\n        {\n            int prevRow = fingerTwo / 6;\n            int prevCol = fingerTwo % 6;\n            int currRow = (word[index] - \'A\') / 6;\n            int currCol = (word[index] - \'A\') % 6;\n            int distance = abs(prevRow - currRow) + abs(prevCol - currCol);\n            secondFinger = distance + recursion(index + 1, fingerOne, word[index] - \'A\', size, word);\n        }\n        return min(firstFinger, secondFinger);\n    }\npublic:\n    int minimumDistance(string word) {\n        int size = word.size(); \n        return recursion(0, -1, -1, size, word);\n    }\n};\n\n```\n# Code (Memoization {[3D DP]})\n```\nclass Solution {\nprivate:\n    int memoization(int index, int fingerOne, int fingerTwo, int &size, string &word, vector<vector<vector<int>>>&dp)\n    {\n        if(index >= size)\n        {\n            return 0;\n        }\n        if(dp[index][fingerOne + 1][fingerTwo + 1] != -1)\n        {\n            return dp[index][fingerOne + 1][fingerTwo + 1];\n        }\n        int firstFinger = 0;\n        if(fingerOne == -1)\n        {\n            firstFinger = memoization(index + 1, word[index] - \'A\', fingerTwo, size, word, dp);\n        }\n        else\n        {\n            int prevRow = fingerOne / 6;\n            int prevCol = fingerOne % 6;\n            int currRow = (word[index] - \'A\') / 6;\n            int currCol = (word[index] - \'A\') % 6;\n            int distance = abs(prevRow - currRow) + abs(prevCol - currCol);\n            firstFinger = distance + memoization(index + 1, word[index] - \'A\', fingerTwo, size, word, dp);\n        }\n        int secondFinger = 0;\n        if(fingerTwo == -1)\n        {\n            secondFinger = memoization(index + 1, fingerOne, word[index] - \'A\', size, word, dp);\n        }\n        else\n        {\n            int prevRow = fingerTwo / 6;\n            int prevCol = fingerTwo % 6;\n            int currRow = (word[index] - \'A\') / 6;\n            int currCol = (word[index] - \'A\') % 6;\n            int distance = abs(prevRow - currRow) + abs(prevCol - currCol);\n            secondFinger = distance + memoization(index + 1, fingerOne, word[index] - \'A\', size, word, dp);\n        }\n        return dp[index][fingerOne + 1][fingerTwo + 1] = min(firstFinger, secondFinger);\n    }\npublic:\n    int minimumDistance(string word) {\n        int size = word.size(); \n        vector<vector<vector<int>>>dp(size, vector<vector<int>>(30, vector<int>(30, -1)));\n        return memoization(0, -1, -1, size, word, dp);\n    }\n};\n\n```
['String', 'Dynamic Programming', 'Recursion', 'Memoization', 'C++']