Python || DP with explanation
The simple intuition is that either the first finger can type a character or the second finger can type a character and we have to just check if it\'s the first
The simple intuition is that either the first finger can type a character or the second finger can type a character and we have to just check if it\'s the first character that either finger is going to type because in that case the distance cost is 0.\nRecursively call and memoize the different states and in each function call update the position of the fingers\n```\nclass Solution:\n    \n    def f(self, i, f1, f2, word, xy):\n        if i==len(word):\n            return 0\n        if self.dp[(i, f1, f2)] != -1:\n            return self.dp[(i, f1, f2)]\n        \n        f1_cost = self.f(i+1, xy[word[i]], f2, word, xy)\n        f2_cost = self.f(i+1, f1, xy[word[i]], word, xy)\n        if f1!=(-1, -1):\n            f1_cost += abs(xy[word[i]][0]-f1[0]) + abs(xy[word[i]][1]-f1[1])\n        if f2!=(-1, -1):\n            f2_cost += abs(xy[word[i]][0]-f2[0]) + abs(xy[word[i]][1]-f2[1])  \n        # print(i, f1, f2, f1_cost, f2_cost)\n        \n        self.dp[(i, f1, f2)] = min(f1_cost, f2_cost)\n        return self.dp[(i, f1, f2)]\n        \n    \n    def minimumDistance(self, word: str) -> int:\n        \n        xy = dict()\n        x = 0\n        y = 0\n        for i in range(65, 65+26):\n            xy[chr(i)] = (x, y)\n            y += 1\n            if y==6:\n                y = 0\n                x += 1\n        # print(xy)\n        self.dp = defaultdict(lambda: -1)\n        return self.f(0, (-1, -1), (-1, -1), word, xy)\n```
['Dynamic Programming', 'Memoization', 'Python']