O(n) Java DP neat soluton
dp solution and short-path solution is actually the same graph model. move finger one by one. There are two choices when we move fingers, one is we just move th
dp solution and short-path solution is actually the same graph model. move finger one by one. There are two choices when we move fingers, one is we just move the finger  which is already in the word to the next character in the word, the other choice is to move the other finger which is maybe not in the word\'s character but is nearer with the next character.\nhttps://leetcode.com/problems/minimum-distance-to-type-a-word-using-two-fingers/discuss/478315/dijkstra-algorithm-with-Priority-Queue\n```\nclass Solution {\n    public int minimumDistance(String word) {\n        // dp[i][j] means when i have printed the first i characters, the other finger is at j (0-26) key what the minimum cost is.\n        int[][] dp = new int[word.length()][26];\n        for (int i = 1; i < word.length(); i++){\n            for (int j = 0; j < 26; j++){\n                    // just move `i - 1` th character in word to `i`th character\n                    // keep the other finger is in j key unchanged\n                    dp[i][j] = dp[i - 1][j] + cost(word.charAt(i) - \'A\', word.charAt(i - 1) - \'A\');\n                    if (word.charAt(i - 1) - \'A\' == j){\n                        // if j key is just the i - 1 character, we can also keep the finger at i - 1 character unchanged, move the other finger to i character, in this case we have 26 choices\n                         for (int k = 0; k < 26; k++){\n                            dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + cost(k, word.charAt(i) - \'A\'));\n                        }\n                    }\n                }\n            }\n        int mini = Integer.MAX_VALUE;\n        for (int i = 0; i < 26; i++){\n                if (dp[word.length() -1][i] < mini){\n                    mini = dp[word.length() - 1][i];\n                }\n        }\n        return mini;\n    }\n    int cost(int a, int b){\n        return Math.abs(a % 6 - b % 6) + Math.abs(a / 6 - b / 6);\n    }\n}\n ```
[]