Javascript and C++ solutions
Synopsis:\n\nWe can use either one or two fingers.  Return the minimum distance for each finger for each character index i of word via top-down  recursion.\n\nN
**Synopsis:**\n\nWe can use either `one` or `two` fingers.  Return the minimum distance for each finger for each character index `i` of `word` via top-down  recursion.\n\n**Note:** for each character, we can find the corresponding row and column by dividing by 6 and modulus by 6 respectively.  The `cost` for moving a finger from the beginning character `beg` to the ending character `end` is the sum of absolute differences between their rows and columns unless it is the first time a finger is used, in which case the cost is `0`, since its free to use a finger for the first time.\n\n**Javascript Solutions:**\n\n*Javascript: Top-Down TLE without memo*\n```\nlet minimumDistance = word => {\n    let cost = (beg, end) => {\n        if (beg == Infinity) // first time finger used\n            return 0;\n        let a = beg.charCodeAt(0) - 65, // offset \'A\' to 0\n            b = end.charCodeAt(0) - 65;\n        let f = Math.floor, abs = Math.abs;\n        return abs(f(a / 6) - f(b / 6)) + abs(f(a % 6) - f(b % 6));\n    };\n    let go = (word, i = 0, one = Infinity, two = Infinity) => {\n        if (i == word.length)\n            return 0;\n        return Math.min(\n            cost(one, word[i]) + go(word, i + 1, word[i], two),\n            cost(two, word[i]) + go(word, i + 1, one, word[i])\n        );\n    };\n    return go(word);\n};\n```\n\n*Javascript: Top-Down with memo*\n```\nlet minimumDistance = word => {\n    let cost = (beg, end) => {\n        if (beg == Infinity) // first time finger used\n            return 0;\n        let a = beg.charCodeAt(0) - 65, // offset \'A\' to 0\n            b = end.charCodeAt(0) - 65;\n        let f = Math.floor, abs = Math.abs;\n        return abs(f(a / 6) - f(b / 6)) + abs(f(a % 6) - f(b % 6));\n    };\n    let go = (word, i = 0, one = Infinity, two = Infinity, memo = {}) => {\n        if (i == word.length)\n            return 0;\n        let key = `${i},${one},${two}`;\n        if (memo[key])\n            return memo[key];\n        return memo[key] = Math.min(\n            cost(one, word[i]) + go(word, i + 1, word[i], two, memo),\n            cost(two, word[i]) + go(word, i + 1, one, word[i], memo)\n        );\n    };\n    return go(word);\n};\n```\n\n**C++ Solutions:**\n\n*C++: Top-Down TLE without memo*\n```\nclass Solution {\npublic:\n    int minimumDistance(string word) {\n        return go(word);\n    }\nprivate:\n    static constexpr char INF = -1;\n    int cost(char beg, char end) {\n        if (beg == INF) // first time finger used\n            return 0;\n        auto a = beg - \'A\', // offset \'A\' to 0\n             b = end - \'A\';\n        return abs((a / 6) - (b / 6)) + abs((a % 6) - (b % 6));\n    }\n    int go(string& s, int i = 0, char one = INF, char two = INF) {\n        if (i == s.size())\n            return 0;\n        return min(\n            cost(one, s[i]) + go(s, i + 1, s[i], two),\n            cost(two, s[i]) + go(s, i + 1, one, s[i])\n        );\n    }\n};\n```\n\n*C++: Top-Down with memo*\n```\nclass Solution {\npublic:\n    int minimumDistance(string word) {\n        return go(word);\n    }\nprivate:\n    static constexpr char INF = -1;\n    int cost(char beg, char end) {\n        if (beg == INF) // first time finger used\n            return 0;\n        auto a = beg - \'A\', // offset \'A\' to 0\n             b = end - \'A\';\n        return abs((a / 6) - (b / 6)) + abs((a % 6) - (b % 6));\n    }\n    using Memo = unordered_map<string, int>;\n    int go(string& s, int i = 0, char one = INF, char two = INF, Memo&& memo = {}) {\n        if (i == s.size())\n            return 0;\n        stringstream key; key << i << "," << one << "," << two;\n        if (memo.find(key.str()) != memo.end())\n            return memo[key.str()];\n        return memo[key.str()] = min(\n            cost(one, s[i]) + go(s, i + 1, s[i], two, move(memo)),\n            cost(two, s[i]) + go(s, i + 1, one, s[i], move(memo))\n        );\n    }\n};\n```
[]