[leetcode] Shortest Word Distance I && II && 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.

Easy question, record the index when word equals to word1 or word2.

class Solution {
public:
    int shortestDistance(vector<string>& words, string word1, string word2) {
        int i, j;
        int ans = words.size();
        i = j = -1;
        for(int k = 0; k < words.size(); k++){
            string word = words[k];
            if(word == word1){
                i = k;
            }
            else if(word == word2){
                j = k;
            }
            if(i != -1 && j != -1){
                ans = min(ans, abs(i - j));
            }
        }
        return ans;
    }
};

 

Shortest Word Distance II

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.

Store the indexes of words in hash table.

Search the indexes in each query.

 

class WordDistance {
public:
    int MAXINT = 0x7fffffff;
    unordered_map<string, vector<int> > map;
    WordDistance(vector<string>& words) {
        for(int i = 0; i < words.size(); i++){
            if(map.find(words[i]) == map.end()){
                //first apperance 
                map[words[i]] = vector<int>(1, i);
            }
            else{
                map[words[i]].push_back(i);
            }
        }
    }

    int shortest(string word1, string word2) {
        vector<int> idxs1 = map[word1];
        vector<int> idxs2 = map[word2];
        int dis = MAXINT;
        int i = 0;
        int j = 0;
        while(i < idxs1.size() && j < idxs2.size()){
            dis = min(dis, abs(idxs1[i] - idxs2[j]));
            if(idxs1[i] < idxs2[j]){
                i++;
            }
            else{
                j++;
            }
        }
        return dis;
    }
};


// Your WordDistance object will be instantiated and called as such:
// WordDistance wordDistance(words);
// wordDistance.shortest("word1", "word2");
// wordDistance.shortest("anotherWord1", "anotherWord2");jmo. 

 

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.

add an if condition to differentiate word1 == word2 and word1 != word2.

class Solution {
public:
    int MAXINT = 0x7fffffff;
    int shortestWordDistance(vector<string>& words, string word1, string word2) {
        int i = -1;
        int j = -1;
        int dis = MAXINT;
        for(int k = 0; k < words.size(); k++){
            string word = words[k];
            if(word == word1){
                if(word1 == word2 && i != -1){
                    dis = min(dis, k - i);
                }
                i = k;
            }
            else if(word == word2){
                j = k;
            }
            if(i != -1 && j != -1){
                dis = min(dis, abs(i - j));
            }
        }
        return dis;
    }
};

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.