Java DP solution !!!
\n    int dist(int a, int b) {\n        if(a == 26 || b == 26) return 0;\n        int x1 = (a)/6, y1 = (a)%6;\n        int x2 = (b)/6, y2 = (b)%6;\n        retu
```\n    int dist(int a, int b) {\n        if(a == 26 || b == 26) return 0;\n        int x1 = (a)/6, y1 = (a)%6;\n        int x2 = (b)/6, y2 = (b)%6;\n        return Math.abs(x2-x1) + Math.abs(y2-y1);        \n    }\n\n    int dfs(String w, int idx, int f1, int f2, int[][][] dp) {\n        if(idx == w.length()) return 0;\n        \n        if(dp[idx][f1][f2] == 0) {\n            int nxt = w.charAt(idx) - \'A\';\n            dp[idx][f1][f2] = Math.min(dist(f1, nxt) + dfs(w, idx + 1, nxt, f2, dp), dist(f2, nxt) + dfs(w, idx + 1, f1, nxt, dp));\n        }\n        \n        return dp[idx][f1][f2];\n    }\n    \n    public int minimumDistance(String w) {\n        int N = w.length();\n        int[][][] dp = new int[N][27][27];\n        return dfs(w, 0, 26, 26, dp);\n    }\n```
[]