[Python3] top-down dp
Algo\nWe use dp to solve this problem. Define fn(i, f1, f2) as the number of movements of typing word[i:] when two fingers are at f1 and f2. \n\nImplementation\
**Algo**\nWe use dp to solve this problem. Define `fn(i, f1, f2)` as the number of movements of typing `word[i:]` when two fingers are at `f1` and `f2`. \n\n**Implementation**\n```\nclass Solution:\n    def minimumDistance(self, word: str) -> int:\n        word = [ord(x)-65 for x in word]\n        dist = lambda x, y: 0 if -1 in (x, y) else abs(x//6 - y//6) + abs(x%6 - y%6) # l1 distance\n        \n        @cache\n        def fn(i, f1=-1, f2=-1):\n            """Return minimum distance of typing word[i:] with 2 fingers."""\n            if i == len(word): return 0 \n            return min(dist(word[i], f1) + fn(i+1, word[i], f2), dist(word[i], f2) + fn(i+1, f1, word[i]))\n        \n        return fn(0)\n```
['Python3']