public int shortest(String word1, String word2) {
            if (word1 == null || word2 == null) return -1;
            ArrayList<Integer> list1 = map.get(word1);
            ArrayList<Integer> list2 = map.get(word2);
            int i = 0, j = 0, minDist = Integer.MAX_VALUE;
            while (i < list1.size() && j < list2.size()) {
                int p1 = list1.get(i), p2 = list2.get(j);
                minDist = Math.min(minDist, Math.abs(p1 - p2));
                if (p1 < p2) ++i;
                else ++j;
            }
            return minDist;
        }