c++ dp 8ms
\n    int minimumDistance(string w) {\n        int n = w.size();\n        vector> dp(n, vector(27, 0));\n        \n        for(int i=1;i<n;++i)\n            for
\n    int minimumDistance(string w) {\n        int n = w.size();\n        vector<vector<int>> dp(n, vector<int>(27, 0));\n        \n        for(int i=1;i<n;++i)\n            for(int c=0;c<27;++c)\n                dp[i][c] = min(cost(w[i]-\'A\', w[i-1]-\'A\') + dp[i-1][c], cost(c, w[i-1]-\'A\') + dp[i-1][w[i]-\'A\']);\n                \n        return dp[n-1][26];\n    }\n    \n    int cost(int i, int j) {\n        if(i == 26) return 0;\n        return abs(i/6 - j/6) + abs(i%6 - j%6);\n    }\n    \n    int threeFinger(string w) {\n        int n = w.size();\n        vector<vector<vector<int>>> dp(n, vector<vector<int>>(27, vector<int>(27,0)));\n        \n        for(int i=1;i<n;++i)\n            for(int c1=0;c1<27;++c1)\n                for(int c2=0;c2<27;++c2)\n                    dp[i][c1][c2] = min({cost(w[i]-\'A\', w[i-1]-\'A\')+dp[i-1][c1][c2], cost(c1, w[i-1]-\'A\')+dp[i-1][w[i]-\'A\'][c2],\n                                        cost(c2, w[i-1]-\'A\')+dp[i-1][c1][w[i]-\'A\']});\n            \n        return dp[n-1][26][26];\n    }\n
[]