dijkstra algorithm with Priority Queue
It\'s quite straightforward to convert this problem to a short-path problem.The state is two finger \'s postition.\n\nclass State implements Comparable<State>{\
It\'s quite straightforward to convert this problem to a short-path problem.The state is two finger \'s postition.\n```\nclass State implements Comparable<State>{\n    public int index;\n    public int otherFinger;\n    public int cost;\n    public State(int i, int o, int c){\n        index = i;\n        otherFinger = o;\n        cost = c;\n    }\n    @Override\n    public int hashCode(){\n        return Objects.hash(index, otherFinger);\n    }\n    @Override\n    public boolean equals(Object o){\n        return index == ((State)o).index && otherFinger == ((State)o).otherFinger;\n    }\n    @Override \n    public int compareTo(State s){\n        return Integer.compare(cost, s.cost);\n    }\n}\n\nclass Solution {\n    public int minimumDistance(String word) {\n        PriorityQueue<State> pq = new PriorityQueue<>();\n        for (int i = 0; i < 26; i++){\n            pq.add(new State(0, i, 0));\n        }\n        Set<State> visited = new HashSet<>();\n        while(!pq.isEmpty()){\n            State s = pq.poll();\n            if (visited.contains(s)){\n                continue;\n            }\n            visited.add(s);\n            if (s.index == word.length() - 1){\n                return s.cost;\n            }\n            pq.add(new State(s.index + 1, s.otherFinger, cost(word.charAt(s.index) - \'A\', word.charAt(s.index + 1) - \'A\') + s.cost));\n            pq.add(new State(s.index + 1, word.charAt(s.index) - \'A\', cost(s.otherFinger, word.charAt(s.index + 1) - \'A\') + s.cost));\n        }\n        return -1;\n    }\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    \n```
[]