Python Bottom up DP solution using dictionaries
\nclass Solution:\n    def __init__(self):\n        self.table = dict()\n        for i, a in enumerate(string.ascii_uppercase):\n            self.table[a] = div
```\nclass Solution:\n    def __init__(self):\n        self.table = dict()\n        for i, a in enumerate(string.ascii_uppercase):\n            self.table[a] = divmod(i, 6)\n    \n    def get_dist(self, a, b):\n        if not a or not b: return 0\n        return abs(self.table[a][0] - self.table[b][0]) + abs(self.table[a][1] - self.table[b][1])\n    \n    def minimumDistance(self, word: str) -> int:\n        if len(word) == 2: return 0\n        table = dict()\n        for i, a in enumerate(string.ascii_uppercase):\n            table[a] = (i // 6, i % 6)\n        \n        dp = {(\'\', \'\'): 0}\n        \n        for c in word:\n            dp2 = dict()\n            for key, value in dp.items():\n                for i in range(2):\n                    new_key = tuple(sorted((key[i], c)))\n                    new_val = value + self.get_dist(key[1 - i], c)\n                    dp2.setdefault(new_key, new_val)\n                    dp2[new_key] = min(dp2[new_key], new_val)\n            dp = dp2\n        return min(dp.values())\n```
[]