c++ | easy | short
\n\n# Code\n\nclass Solution {\npublic:\n     int minimumDistance(string word) {\n        vector<int> dp(26);\n        int res = 0, save = 0, n = word.size();\n
\n\n# Code\n```\nclass Solution {\npublic:\n     int minimumDistance(string word) {\n        vector<int> dp(26);\n        int res = 0, save = 0, n = word.size();\n        for (int i = 0; i < n - 1; ++i) {\n            int b = word[i] - \'A\', c = word[i + 1] - \'A\';\n            for (int a = 0; a < 26; ++a)\n                dp[b] = max(dp[b], dp[a] + d(b, c) - d(a, c));\n            save = max(save, dp[b]);\n            res += d(b, c);\n        }\n        return res - save;\n    }\n\n    int d(int a, int b) {\n        return abs(a / 6 - b / 6) + abs(a % 6 - b % 6);\n    }\n};\n```
['C++']