Simple Java solution using memoization
Straightforward solution using meomization.  fpos indicates position of first finger, rpos indicates position of the second finger. We just recurse and memoize 
Straightforward solution using meomization.  fpos indicates position of first finger, rpos indicates position of the second finger. We just recurse and memoize the states.\n\n```\nclass Solution {\n    public int minimumDistance(String word) {\n        Map<String, Integer> memo = new HashMap();\n        return dfs(word, 0, -1, 1,memo);\n    }\n    \n    int dfs(String word, int fpos, int rpos, int pos, Map<String, Integer> memo) {\n        String key = fpos + " : " +  rpos +  ":" + pos;\n        if(memo.containsKey(key)) return memo.get(key);\n        if(pos == word.length()) return 0;\n        \n        int cost2 = 0;\n        if(rpos != -1) {\n            cost2 = diff(word.charAt(rpos), word.charAt(pos));\n        }\n        int cost1 = diff(word.charAt(fpos), word.charAt(pos));\n       \n        int c1 = dfs(word, pos, rpos, pos+1, memo) + cost1;\n        int c2 = dfs(word, fpos, pos, pos+1, memo) + cost2;\n        int val = Math.min(c1, c2);\n    \n        memo.put(key, val);\n        return val;\n    }\n    \n    int diff(char ch1, char ch2) {\n        int p1 = ch1 - \'A\';\n        int x1 = p1/6;\n        int y1 = p1%6;\n        \n        int p2 = ch2 - \'A\';\n        int x2 = p2/6;\n        int y2 = p2%6;\n        \n        return Math.abs(x1-x2) + Math.abs(y1-y2);\n    }\n}\n```
[]