Java Solution -- like state dp
state dp is dp[i][state][eq], this problem is dp[i][state]\n\n\nclass Solution {\n    public int minimumDistance(String word) {\n        int n = word.length();\
state dp is dp[i][state][eq], this problem is dp[i][state]\n\n```\nclass Solution {\n    public int minimumDistance(String word) {\n        int n = word.length();\n        int[][] dis = new int[27][27];\n        for(int i = 0; i < 26; i++) {\n            for(int j = 0; j < 26; j++) {\n                dis[i+1][j+1] = Math.abs(i / 6 - j / 6) + Math.abs(i % 6 - j % 6);\n            }\n        }\n        int[][][] dp = new int[n][27][27];\n        return dfs(dp, 0, null, null, word, dis);\n    }\n    \n    private int dfs(int[][][] dp, int i, Character a, Character b, String word, int[][] dis) {\n        if(i == word.length()) return 0;\n        int da = a == null ? 0 : a - \'A\' + 1;\n        int db = b == null ? 0 : b - \'A\' + 1;\n        if(dp[i][da][db] != 0) return dp[i][da][db];\n        char c = word.charAt(i);\n        int dc = c - \'A\' + 1;\n        return dp[i][da][db] = Math.min(dis[da][dc] + dfs(dp, i + 1, c, b, word, dis), dis[db][dc] + dfs(dp, i + 1, a, c, word, dis));\n    }\n}\n```
[]