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
andwords2
will not exceed1000
. - The length of
pairs
will not exceed2000
. - The length of each
pairs[i]
will be2
. - The length of each
words[i]
andpairs[i][j]
will be in the range[1, 20]
.
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
class Solution {
private int[] p;
public boolean areSentencesSimilarTwo(String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
if (sentence1.length != sentence2.length) {
return false;
}
int n = similarPairs.size();
p = new int[n << 1];
for (int i = 0; i < p.length; ++i) {
p[i] = i;
}
Map<String, Integer> words = new HashMap<>();
int idx = 0;
for (List<String> e : similarPairs) {
String a = e.get(0), b = e.get(1);
if (!words.containsKey(a)) {
words.put(a, idx++);
}
if (!words.containsKey(b)) {
words.put(b, idx++);
}
p[find(words.get(a))] = find(words.get(b));
}
for (int i = 0; i < sentence1.length; ++i) {
if (Objects.equals(sentence1[i], sentence2[i])) {
continue;
}
if (!words.containsKey(sentence1[i]) || !words.containsKey(sentence2[i]) || find(words.get(sentence1[i])) != find(words.get(sentence2[i]))) {
return false;
}
}
return true;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}
var p []int
func areSentencesSimilarTwo(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
if len(sentence1) != len(sentence2) {
return false
}
n := len(similarPairs)
p = make([]int, (n<<1)+10)
for i := 0; i < len(p); i++ {
p[i] = i
}
words := make(map[string]int)
idx := 1
for _, e := range similarPairs {
a, b := e[0], e[1]
if words[a] == 0 {
words[a] = idx
idx++
}
if words[b] == 0 {
words[b] = idx
idx++
}
p[find(words[a])] = find(words[b])
}
for i := 0; i < len(sentence1); i++ {
if sentence1[i] == sentence2[i] {
continue
}
if words[sentence1[i]] == 0 || words[sentence2[i]] == 0 || find(words[sentence1[i]]) != find(words[sentence2[i]]) {
return false
}
}
return true
}
func find(x int) int {
if p[x] != x {
p[x] = find(p[x])
}
return p[x]
}