Problem description:

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. Your method will be called repeatedly many times with different parameters.

Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = “makes”, word2 = “coding”
Output: 1
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

Solution:

  1. Use a defaultdict to quickly find word
  2. Since a word might appear multiple times, need to search where’s the shortest distance
    1. walk through every index of word1 and word2, takes O(m*n)
    2. use merge sort, take O(m+n)

O(m*n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class WordDistance:

def __init__(self, wordsDict: List[str]):
self.dic = defaultdict(list)
for i in range(len(wordsDict)):
self.dic[wordsDict[i]].append(i)

def shortest(self, word1: str, word2: str) -> int:
dis1, dis2 = self.dic[word1], self.dic[word2]
res = float('inf')
for v1 in dis1:
for v2 in dis2:
res = min(res, abs(v1-v2))
return res

# Your WordDistance object will be instantiated and called as such:
# obj = WordDistance(wordsDict)
# param_1 = obj.shortest(word1,word2)

O(m+n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class WordDistance:

def __init__(self, wordsDict: List[str]):
self.dic = defaultdict(list)
for i in range(len(wordsDict)):
self.dic[wordsDict[i]].append(i)

def shortest(self, word1: str, word2: str) -> int:
dis1, dis2 = self.dic[word1], self.dic[word2]
i = j = 0
res = float('inf')
while i < len(dis1) and j < len(dis2):
res = min(res, abs(dis1[i] - dis2[j]))
if dis1[i] < dis2[j]:
i += 1
else:
j += 1
return res

The problem is relatively simple, but this is a design question, so you have to collect as much information as you need from the interviewer. For example, does the input contains any duplicate elements? if it does, we need to use a vector<int> to store the index of same word.

basic solution:

in the search part, the time complexity is O(MN)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
WordDistance(vector<string>& words) {
for (int i = 0; i < words.size(); ++i) {
m[words[i]].push_back(i);
}
}

int shortest(string word1, string word2) {
int res = INT_MAX;
for (int i = 0; i < m[word1].size(); ++i) {
for (int j = 0; j < m[word2].size(); ++j) {
res = min(res, abs(m[word1][i] - m[word2][j]));
}
}
return res;
}

time complexity: O(MN)

improved solution:

We can improve the above solution by using two pointers to find the smallest result.

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
class WordDistance {
public:
unordered_map<string, vector<int>> map;
WordDistance(vector<string> words) {
for(int i= 0; i< words.size(); i++){
map[words[i]].push_back(i);
}
}

int shortest(string word1, string word2) {
auto vec1= map[word1];
auto vec2= map[word2];
int res= INT_MAX;
for(int i= 0, j= 0; i< vec1.size()&& j< vec2.size();){
int idx1= vec1[i];
int idx2= vec2[j];
if(idx1< idx2){
res= min(res, idx2-idx1);
i++;
}
else{
res= min(res, idx1-idx2);
j++;
}
}
return res;
}
};

time complexity: O(m+n)

more concise solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class WordDistance {
public:
WordDistance(vector<string>& words) {
for (int i = 0; i < words.size(); ++i) {
m[words[i]].push_back(i);
}
}

int shortest(string word1, string word2) {
int i = 0, j = 0, res = INT_MAX;
while (i < m[word1].size() && j < m[word2].size()) {
res = min(res, abs(m[word1][i] - m[word2][j]));
m[word1][i] < m[word2][j] ? ++i : ++j;
}
return res;
}

private:
unordered_map<string, vector<int> > m;
};

time complexity: O(m+n)
reference:
https://goo.gl/BkmZXc
http://www.cnblogs.com/grandyang/p/5187640.html