Python 3 - DP memoization, pythonic
python\nclass Solution:\n    def minimumDistance(self, word: str) -> int:\n        table = defaultdict(tuple)\n        for i in range(26):\n            c = chr(
```python\nclass Solution:\n    def minimumDistance(self, word: str) -> int:\n        table = defaultdict(tuple)\n        for i in range(26):\n            c = chr(ord(\'A\') + i)\n            row, col = divmod(i, 6)\n            table[c] = (row, col)\n        n = len(word)\n            \n        def get_dist(c1, c2):\n            if c1 == \'#\' or c2 == \'#\':\n                return 0\n            r1, c1 = table[c1]\n            r2, c2 = table[c2]\n            return abs(r1-r2) + abs(c1-c2)\n        \n        @lru_cache(None)\n        def dp(i, f1, f2):\n            if i == n:\n                return 0\n            c = word[i]\n            d1, d2 = get_dist(c, f1), get_dist(c, f2)\n            return min(d1 + dp(i + 1, c, f2), d2 + dp(i + 1, f1, c))\n        \n        return dp(0, \'#\', \'#\')\n```
[]