Python simple recursion DP solution (Beats 100% space and time)
\nclass Solution:\n    def __init__(self):\n        self.dic={}\n        self.h={}\n    def minimumDistance(self, word: str) -> int:\n        chars=["ABCDEF","G
```\nclass Solution:\n    def __init__(self):\n        self.dic={}\n        self.h={}\n    def minimumDistance(self, word: str) -> int:\n        chars=["ABCDEF","GHIJKL","MNOPQR","STUVWX","YZ"]\n        temp1=0\n        temp2=0\n        for i in chars:\n            temp2=0\n            for j in i:\n                self.dic[j]=(temp1,temp2)\n                temp2+=1\n            temp1+=1\n        return self.recursion(word,1,0,self.dic[word[0]],(-1,-1))\n        \n        \n        \n        \n    def recursion(self,word,index,value,previndex,previndex2):\n        if (index, previndex, previndex2) in self.h:\n            return value+self.h[(index, previndex, previndex2)]\n        if index>=len(word):\n            return value\n        valF1=abs(previndex[0]-self.dic[word[index]][0]) + abs(previndex[1]-self.dic[word[index]][1])\n        if previndex2==(-1,-1):\n            valF2 = 0\n        else:\n            valF2=abs(previndex2[0]-self.dic[word[index]][0]) + abs(previndex2[1]-self.dic[word[index]][1])\n        choiceF1=self.recursion(word,index+1,value+valF1,self.dic[word[index]],previndex2)\n        choiceF2=self.recursion(word,index+1,value+valF2,previndex,self.dic[word[index]])\n        res = min(choiceF1, choiceF2)\n        self.h[(index, previndex, previndex2)] = res-value\n        return res\n```
[]