Sunday, September 13, 2015

Day 126, #243 #244 #245 Shortest Word Distance, Shortest Word Distance II, Shortest Word Distance III

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.
----------------------------------------------------------------------------
class Solution {
public:
    int shortestDistance(vector<string>& words, string word1, string word2) {
        int shortest = words.size();
        int pos1 = -words.size(), pos2 = -words.size();
        for (int i = 0; i < words.size(); i++) {
            if (words[i] == word1) {
                pos1 = i;
                shortest = min(shortest,pos1 - pos2);
            }
            if (words[i] == word2) {
                pos2 = i;
                shortest = min(shortest,pos2 - pos1);
            }
        }
        return shortest;
    }
};

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.
-----------------------------------------------------------------------

class WordDistance {
public:
    WordDistance(vector<string>& words) {
        for (int i = 0; i < words.size(); i++) {
            if (dic.find(words[i]) == dic.end()) {
                vector<int> t = {i};
                dic[words[i]] = t;
            }else {
                dic[words[i]].push_back(i);
            }
        }
    }

    int shortest(string word1, string word2) {
        int i = 0, j = 0;
        int distance = INT_MAX;
        
        while (i < dic[word1].size() && j < dic[word2].size()) {
            distance = min(distance,abs(dic[word1][i] - dic[word2][j]));
            if (dic[word1][i] < dic[word2][j]) {
                i++;
            }else {
                j++;
            }
        }
        
        return distance;
    }
private:
    unordered_map<string,vector<int>> dic;
};


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

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.
---------------------------------------------------------------------
class Solution {
public:
    int sameWords(vector<string>& words, string word) {
        int distance = words.size();
        int pre = -words.size();
        for (int i = 0; i < words.size(); i++) {
            if (words[i] == word) {
                distance = min(distance,i - pre);
                pre = i;
            }
        }
        return distance;
    }

    int shortestWordDistance(vector<string>& words, string word1, string word2) {
        if (word1 == word2) return sameWords(words,word1);
        int pos1 = -words.size(), pos2 = -words.size();
        int shortest = words.size();
        for (int i = 0; i < words.size(); i++) {
            if (words[i] == word1) {
                shortest = min(shortest,i - pos2);
                pos1 = i;
            }else if (words[i] == word2) {
                shortest = min(shortest,i - pos1);
                pos2 = i;
            }
        }
        
        return shortest;
    }
};

No comments:

Post a Comment