Interviewees said this question(and follow up) is the same as Leetcode 243
a.optimize it if the function will be called multiple times (most likely LC 244)
b.two words can be the same
Shortest Word Distance

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.
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.
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.
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>

using namespace std;

int shortestWordDistance1(vector<string>& words, string w1, string w2) {
    int idx1 = -1, idx2 = -1;
    int res = words.size();
    for (int i = 0; i < words.size(); i++) {
        if (words[i] == w1) idx1 = i;
        if (words[i] == w2) idx2 = i;
        if (idx1 != -1 && idx2 != -1)
            res = min(res, abs(idx1 - idx2));
    }
    return res;
}

unordered_map <string, vector<int>> mymap;
void preprocess(vector<string>& words) {
    for (int i = 0; i < words.size(); i++)
        mymap[words[i]].push_back(i);
}

int shortestWordDistance2(vector<string>& words, string w1, string w2) {
    int res = words.size();
    int i = 0, j = 0;
    while (i < mymap[w1].size() && j < mymap[w2].size()) {
        res = min(res, abs(mymap[w1][i] - mymap[w2][j]));
        if (mymap[w1][i] < mymap[w2][j]) i++;
            else j++;
    }
    return res;
}

int shortestWordDistance3(vector<string>& words, string w1, string w2) {
    int idx1 = -1, idx2 = -1, res = words.size();
    for (int i = 0; i < words.size(); i++) {
        if (words[i] == w1) idx1 = i;
        if (words[i] == w2) {
            if (w1 == w2) idx1 = idx2;
            idx2 = i;
        }
        if (idx1 != -1 && idx2 != -1)
            res = min(res, abs(idx1 - idx2));
    }
    return res;
}

int main(int argc, char *argv[]) {
    vector<string> words = {"Indeed", "use", "python", "and", "java", "to", "deal", "with", "the", "python", "python", "and", "is", "for", "java"};
    cout<<shortestWordDistance1(words, "python", "java")<<endl;
    preprocess(words);
    cout<<shortestWordDistance2(words, "python", "java")<<endl;
    cout<<shortestWordDistance3(words, "python", "python")<<endl;
}

results matching ""

    No results matching ""