LeetCode Shortest Word Distance I II and III

Tags: , ,

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

 

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:

 

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:

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