Memorized Search (DP) by recursion, Easy Understanding
\nclass Solution {\n    int[][][] dp = new int[27][27][300];\n    public int minimumDistance(String word) {\n        return helper(word, 0, null, null);\n    }\
```\nclass Solution {\n    int[][][] dp = new int[27][27][300];\n    public int minimumDistance(String word) {\n        return helper(word, 0, null, null);\n    }\n    //Memorized Search, next step of pos is either moving c1 or c2 to pos +1;  then calculate from end of string.\n    private int helper(String word, int pos, Character c1, Character c2) {\n        if (pos >= word.length()) return 0;  // check pos to avoid stringIndex exception\n        Character curr = word.charAt(pos);\n        int i1 = c1 == null ? 26 : c1 - \'A\';   // must check null before call dp[i1][i2][pos]; use 26 to represent null;\n        int i2 = c2 == null ? 26 : c2 - \'A\';\n        if (dp[i1][i2][pos] == 0) {\n            dp[i1][i2][pos] = Math.min(dist(c1, curr) + helper(word, pos + 1, curr, c2),\n                                       dist(c2, curr) + helper(word, pos + 1, c1, curr));\n        }\n        return  dp[i1][i2][pos];\n    }\n\n    private int dist(Character a, Character b){\n        if (a == null || b == null) return 0;\n        int[] ia = new int[]{(a - \'A\') / 6, (a - \'A\') % 6};\n        int[] ib = new int[]{(b - \'A\') / 6, (b - \'A\') % 6};\n        int c = ia[0] - ib[0];\n        c = c < 0 ? -c : c;\n        int d = ia[1] - ib[1];\n        d = d < 0 ? -d : d;\n        return c + d; \n    }\n}\n```
[]