o(n) space
inspired by \nhttps://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/discuss/477659/4%2B-DP-Solutions\n\ni think there is an o(n) space
inspired by \nhttps://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/discuss/477659/4%2B-DP-Solutions\n\ni think there is an o(n) space method, just like this,\n```\n    int cost(int from, int to) {\n        if (from == 26)\n            return 0;\n\n        return abs(from / 6 - to / 6) + abs(from % 6 - to % 6);\n    }\n\n    int minimumDistance(string word) {\n        int dp[2][27];\n        fill_n(&dp[0][0], 2 * 27, 0);\n\n        int n = word.size();\n        for (int pos = n - 1; pos >= 1; --pos)\n            for (int i = 0; i < 27; ++i) {\n                int to = word[pos] - \'A\';\n                int j = word[pos - 1] - \'A\';\n                dp[pos % 2][i] = min(cost(i, to) + dp[(pos + 1) % 2][j], cost(j, to) + dp[(pos + 1) % 2][i]);\n            }\n\n        return dp[1][26];\n    }\n```
[]