Friday, October 9, 2020

737. Sentence Similarity II ~~~~~~~~M !!!!

Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar.

For example, words1 = ["great", "acting", "skills"] and words2 = ["fine", "drama", "talent"] are similar, if the similar word pairs are pairs = [["great", "good"], ["fine", "good"], ["acting","drama"], ["skills","talent"]].

Note that the similarity relation is transitive. For example, if "great" and "good" are similar, and "fine" and "good" are similar, then "great" and "fine" are similar.

Similarity is also symmetric. For example, "great" and "fine" being similar is the same as "fine" and "great" being similar.

Also, a word is always similar with itself. For example, the sentences words1 = ["great"], words2 = ["great"], pairs = [] are similar, even though there are no specified similar word pairs.

Finally, sentences can only be similar if they have the same number of words. So a sentence like words1 = ["great"] can never be similar to words2 = ["doubleplus","good"].

Note:

  • The length of words1 and words2 will not exceed 1000.
  • The length of pairs will not exceed 2000.
  • The length of each pairs[i] will be 2.
  • The length of each words[i] and pairs[i][j] will be in the range [1, 20].

A:

这是典型的disjoint set forests 问题。See CLRS , chapter 21.3, page 571

就是构建朋友关系, 每个人,映射其所有的朋友放道一个Set里


Union-Find   也可以map 每一个string 到一个共同的祖先(用数字表示)

Approach #2: Union-Find [Accepted]

Intuition

As in Approach #1, we want to know if there is path connecting two words from edges represented by pairs.

Our problem comes down to finding the connected components of a graph. This is a natural fit for a Disjoint Set Union (DSU) structure.

Algorithm

Draw edges between words if they are similar. For easier interoperability between our DSU template, we will map each word to some integer ix = index[word]. Then, dsu.find(ix) will tell us a unique id representing what component that word is in.

For more information on DSU, please look at Approach #2 in the article here. For brevity, the solutions showcased below do not use union-by-rank.

After putting each word in pairs into our DSU template, we check successive pairs of words w1, w2 = words1[i], words2[i]. We require that w1 == w2, or w1 and w2 are in the same component. This is easily checked using dsu.find.


class Solution {
public:
    bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<vector<string>>& pairs) {
        int n1 = words1.size();
        if(n1 != words2.size()){
            return false;
        }
        // make_set is default done by map
        unordered_map<string,string> M;
        // union
        for(auto& pair : pairs){
            link_set(pair[0], pair[1], M);
        }
        for(int i =0;i<n1;i++){
            if(find_set(words1[i], M) != find_set(words2[i], M)){
                return false;
            }
        }
        return true;
    }
private:
    string find_set(string str, unordered_map<string,string> &M){
        if(M.find(str) == M.end()){
            M[str] = str; // take care un-seen words  
        }
        while(M[str] != str){
            str = M[str];
        }
        return str;
    }
    void link_set(string str1, string str2, unordered_map<string,string> &M){
        M[find_set(str1, M)] = find_set(str2,M );// originally, I wrote M[str1] = find_set(str2,M ), but wrong
    }
};


Errors:

// originally, I wrote M[str1] = find_set(str2,M ), but wrong



No comments:

Post a Comment