[Python3] 10 lines code, 76 ms, with explaination
Idea: total cost = cost by only using finger1 - cost saved by finger2\n\nfrom functools import lru_cache\nclass Solution:\n    def minimumDistance(self, word: s
Idea: total cost = cost by only using finger1 - cost saved by finger2\n```\nfrom functools import lru_cache\nclass Solution:\n    def minimumDistance(self, word: str) -> int:\n        word = [ord(c)-ord(\'A\') for c in word]                          # char to int\n        @lru_cache(None)\n        def d(a, b):                                                    # store distance between char a, b\n            return abs(a//6 - b//6) + abs(a%6 - b%6)\n        dp = [0]*26                                                     # for each char, store the maximum cost saved by finger2\n        for b, c in zip(word, word[1:]):                                # current saved cost = d(b,c) - d(a,c) (using finger2 instead of finger1)\n            dp[b] = max(dp[a] + d(b,c) - d(a,c) for a in range(26))     # a is previous char typed by finger2, dp[a] is previous saved cost\n        return sum(d(b,c) for b, c in zip(word, word[1:])) - max(dp)    # cost of finger1 - cost saved by finger2\n```
['Dynamic Programming', 'Python']