LeetCode Shortest Word Distance I II and III
Shortest Word Distance I
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
For example, Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = "coding", word2 = "practice", return 3. Given word1 = "makes", word2 = "coding", return 1.
Note
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
Analysis
We can scan the list and use two pointers to record the most recent indexes of the two words. Then we update the distance and the global minimum distance.
Java Solution
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class ShortestDistance { public int shortestDistance(String[] words, String word1, String word2) { int p1 = -1, p2 = -1, minDistance = Integer.MAX_VALUE; for(int i = 0; i < words.length; i++){ if(words[i].equals(word1)){ p1 = i; } if(words[i].equals(word2)){ p2 = i; } if(p1 != -1 && p2 != -1) minDistance = Math.min(minDistance, Math.abs(p1 - p2)); } return minDistance; } } |
Shortest Word Distance II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
For example, Assume that words =
["practice", "makes", "perfect", "coding", "makes"].Given word1 = “coding”, word2 = “practice”, return 3. Given word1 = “makes”, word2 = “coding”, return 1.
Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
Analysis
We can pre scan the list to record the indexes for each word, and store the sorted index list into a HashTable. The key is the word, and the value is the sorted index list. In the query process, the problem becomes to find the minimum difference between the values in the two sorted index lists.
For example, the indexes for word1 might be {1, 5, 9}, and the indexes for word2 might be {4, 11, 15}, then we can easily use the idea of merging sorted lists to get the minimum.
Java solution:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
class WordDistanceII { Map<String, List<Integer>> map = new HashMap<String, List<Integer>>(); public WordDistanceII(String[] words) { for (int i = 0; i < words.length; i++) { String curWord = words[i]; if (map.containsKey(curWord)) { map.get(curWord).add(i); } else { List<Integer> list = map.get(curWord); list.add(i); map.put(curWord, list); } } } public int shortestDistance(String word1, String word2) { List<Integer> indexList1 = map.get(word1); List<Integer> indexList2 = map.get(word2); int minDistance = Integer.MAX_VALUE; int p1 = 0, p2 = 0; while (p1 < indexList1.size() && p2 < indexList2.size()) { int idx1 = indexList1.get(p1); int idx2 = indexList2.get(p2); minDistance = Math.min(minDistance, Math.abs(idx1 - idx2)); if (idx1 < idx2) p1++; else p2++; } return minDistance; } } |
Shortest Word Distance III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
word1 and word2 may be the same and they represent two individual words in the list.
For example, Assume that words =
["practice", "makes", "perfect", "coding", "makes"].Given word1 = “makes”, word2 = “coding”, return 1. Given word1 = “makes”, word2 = “makes”, return 3.
Note: You may assume word1 and word2 are both in the list.
Analysis
Similar to Shortest Word Distance I, we can use two pointers to record the most recent indexes of the two words. To solve the problem that the two words can be the same, we must make sure the two pointers always point to the largest two indexes.
For example, give the word list = {a b a a a b}, and the query (a, a),
If p1, p2 are always pointed to the two largest indexes of word a in the scan, we can guarantee that one of the abs(p1 - p2) is the shortest distance.
Java Solution:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class WordDistanceIII { int shortestWordDistance(String[] words, String word1, String word2) { int p1 = -1, p2 = -1, minDistance = Integer.MAX_VALUE; for(int i=0; i < words.length; ++i){ String curWord = words[i]; if(word1.equals(word2)){ if(curWord.equals(word1)){ // let p1, p2 point to the two largest indexes if(p2 < p1) p2 = i; else p1 = i; } }else{ if(curWord.equals(word1)) p1 = i; if(curWord.equals(word2)) p2 = i; } if(p1 >= 0 && p2 >= 0) minDistance = Math.min(minDistance, Math.abs(p1 - p2)); } return minDistance; } }; |
Reference:
https://segmentfault.com/a/1190000003906667
http://buttercola.blogspot.com/2015/08/leetcode-shortest-word-distance-iii.html
https://discuss.leetcode.com/category/304/shortest-word-distance











