Javascript | DFS with memorization
\nconst dist = function(from, to){\n    if(from==-1) return 0; \n    const d1 = Math.abs((from.charCodeAt(0)-65)%6-(to.charCodeAt(0)-65)%6),\n          d2 = Mat
```\nconst dist = function(from, to){\n    if(from==-1) return 0; \n    const d1 = Math.abs((from.charCodeAt(0)-65)%6-(to.charCodeAt(0)-65)%6),\n          d2 = Math.abs(Math.floor((from.charCodeAt(0)-65)/6)-Math.floor((to.charCodeAt(0)-65)/6));\n    return d1 + d2;\n}\nvar minimumDistance = function(word) {\n\n    const dp = new Map();\n    \n    const dfs = function(i, lpos, rpos){\n        if(i==word.length) \n            return 0;\n        const key = [i,lpos,rpos].join(\',\');\n        if(dp.get(key)) \n            return dp.get(key);\n        dp.set(key, Math.min(dist(lpos,word[i])+dfs(i+1,word[i],rpos), dist(rpos,word[i])+dfs(i+1,lpos,word[i])));\n        return dp.get(key);\n    }\n    return dfs(0, -1, -1);\n};\n```
['Memoization', 'JavaScript']