Sentence Similarity II

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

思路:跟Accounts Merge一個思路,union find,把Pairs裏面string對應的index,merge成相同的,然後進行word1 word2逐個比較。注意兩個點容易犯錯:

1. rootQ = findParents(parents, i), 不是i

2. word1[i].equals(word2[i]) 不用比較hashmap,有可能出現的完全一樣,而不在hashmap裏面。

class Solution {
    public boolean areSentencesSimilarTwo(String[] words1, String[] words2, String[][] pairs) {
        if(words1 == null || words2 == null || pairs == null || words1.length != words2.length){
            return false;
        }
        
        // Union-find algoirthm;
        int[] parents = new int[pairs.length];
        for(int i=0; i<parents.length; i++){
            parents[i] = i;
        }
        HashMap<String, Integer> hashmap = new HashMap<String, Integer>();
        for(int i=0; i<pairs.length; i++){
            for(int j=0; j<pairs[0].length; j++){
                String str = pairs[i][j];
                if(hashmap.containsKey(str)){
                    int rootP = findParent(parents,hashmap.get(str));
                    int rootQ = findParent(parents, i);
                    if(rootP != rootQ){
                        parents[rootQ] = rootP;
                    }
                } else {
                    hashmap.put(str, i);
                }
            }
        }
        
        for(int i=0; i<words1.length; i++){
            if(words1[i].equals(words2[i])){
                continue;
            }
            if(!hashmap.containsKey(words1[i]) || !hashmap.containsKey(words2[i]) ){
                return false;
            }
            if( findParent(parents, hashmap.get(words1[i]))
                         != findParent(parents, hashmap.get(words2[i]))){
                return false;
            }
        }
        return true;
    }
    
    private int findParent(int[] parents, int i){
        while(i!=parents[i]){
            parents[i] = parents[parents[i]];
            i = parents[i];
        }
        return i;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章