##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/737.html

# 737. Sentence Similarity II

Medium

## Description

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

## Solution

First check whether words1 and words2 have the same length. If their lengths are different, return false.

Use two maps to map each word to its group and to map each group to the list of words in the group, respectively.

For each pair of similar words, update both maps. If two words that were originally from different groups are in the same pair, then the two groups are merged into one group, with the smaller group number maintained.

After all pairs are dealt with, loop over both arrays and check whether the two words at the same index are similar. If there exist two words at the same index that are not similar, return false. If all words are similar, return true.

• class Solution {
public boolean areSentencesSimilarTwo(String[] words1, String[] words2, List<List<String>> pairs) {
if (words1 == null || words2 == null || words1.length != words2.length)
return false;
Map<String, Integer> wordGroupMap = new HashMap<String, Integer>();
Map<Integer, List<String>> groupWordsMap = new HashMap<Integer, List<String>>();
int groupCount = 0;
for (List<String> pair : pairs) {
String word1 = pair.get(0), word2 = pair.get(1);
int group1 = wordGroupMap.getOrDefault(word1, -1), group2 = wordGroupMap.getOrDefault(word2, -1);
if (group1 < 0 && group2 < 0) {
wordGroupMap.put(word1, groupCount);
wordGroupMap.put(word2, groupCount);
List<String> list = groupWordsMap.getOrDefault(groupCount, new ArrayList<String>());
groupWordsMap.put(groupCount, list);
groupCount++;
} else if (group2 < 0) {
wordGroupMap.put(word2, group1);
List<String> list = groupWordsMap.getOrDefault(group1, new ArrayList<String>());
groupWordsMap.put(group1, list);
} else if (group1 < 0) {
wordGroupMap.put(word1, group2);
List<String> list = groupWordsMap.getOrDefault(group2, new ArrayList<String>());
groupWordsMap.put(group2, list);
} else {
if (group1 == group2)
continue;
List<String> list1 = groupWordsMap.getOrDefault(group1, new ArrayList<String>());
List<String> list2 = groupWordsMap.getOrDefault(group2, new ArrayList<String>());
if (group1 < group2) {
for (String word : list2)
wordGroupMap.put(word, group1);
groupWordsMap.put(group1, list1);
groupWordsMap.remove(group2);
} else {
for (String word : list1)
wordGroupMap.put(word, group2);
groupWordsMap.put(group2, list2);
groupWordsMap.remove(group1);
}
}
}
int length = words1.length;
for (int i = 0; i < length; i++) {
String word1 = words1[i], word2 = words2[i];
if (word1.equals(word2))
continue;
int group1 = wordGroupMap.getOrDefault(word1, -1), group2 = wordGroupMap.getOrDefault(word2, -1);
if (group1 < 0 || group2 < 0 || group1 != group2)
return false;
}
return true;
}
}

• Todo

• class Solution:
def areSentencesSimilarTwo(
self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]
) -> bool:
if len(sentence1) != len(sentence2):
return False
n = len(similarPairs)
p = list(range(n << 1))

def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]

words = {}
idx = 0
for a, b in similarPairs:
if a not in words:
words[a] = idx
idx += 1
if b not in words:
words[b] = idx
idx += 1
p[find(words[a])] = find(words[b])

for i in range(len(sentence1)):
if sentence1[i] == sentence2[i]:
continue
if (
sentence1[i] not in words
or sentence2[i] not in words
or find(words[sentence1[i]]) != find(words[sentence2[i]])
):
return False
return True