public int shortestWordDistance(Map<String, List<Integer>> wordMap, String word1, String word2) {
        int min = Integer.MAX_VALUE;
        if (word1.equals(word2)) {
            List<Integer> indexList = wordMap.get(word1);
            for (int i = 0; i < indexList.size() - 1; ++i) {
                int i1 = indexList.get(i), i2 = indexList.get(i + 1);
                min = Math.min(Math.abs(i1 - i2), min);
            }
        } else {
            List<Integer> indexList1 = wordMap.get(word1);
            List<Integer> indexList2 = wordMap.get(word2);
            int i1 = 0, i2 = 0;
            while (i1 < indexList1.size() && i2 < indexList2.size()) {
                int tmp1 = indexList1.get(i1), tmp2 = indexList2.get(i2);
                min = Math.min(Math.abs(tmp1 - tmp2), min);
                if (tmp1 < tmp2) {
                    i1++;
                } else {
                    i2++;
                }
            }
        }
        return min;
    }