737. 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]
Solution
(1) Java
class Solution {
class UF {
private int[] parent;
private int[] rank;
public UF(int size) {
parent = new int[size];
for (int i = 0; i < size; i++) {
parent[i] = i;
}
rank = new int[size];
}
public int find(int i) {
while ( i != parent[parent[i]]) {
i = find(parent[i]);
}
return i;
}
public void union(int i, int j) {
int pi = find(i);
int pj = find(j);
if (pi == pj) {
return;
}
if (rank[pi] <= rank[pj]) {
parent[pj] = pi;
} else {
parent[pi] = pj;
}
}
}
public boolean areSentencesSimilarTwo(String[] words1, String[] words2, String[][] pairs) {
if (words1.length == 0 && words2.length == 0){
return true;
} else if (words1.length == 0 || words2.length == 0) {
return false;
} else if (words1.length != words2.length) {
return false;
}
Map<String, Integer> map = new HashMap<>();
int idx = 0;
for (String[] pair : pairs) {
if (!map.containsKey(pair[0])) {
map.put(pair[0], idx);
idx++;
}
if (!map.containsKey(pair[1])) {
map.put(pair[1], idx);
idx++;
}
}
UF uf = new UF(map.size());
for (String[] pair : pairs) {
int idx1 = uf.find(map.get(pair[0]));
int idx2 = uf.find(map.get(pair[1]));
uf.union(idx1, idx2);
}
for (int i = 0; i < words1.length; i++) {
String word1 = words1[i];
String word2 = words2[i];
if (word1.equals(word2)) {
continue;
}
if (map.containsKey(word1) && map.containsKey(word2)) {
int idx1 = map.get(word1);
int idx2 = map.get(word2);
if (uf.find(idx1) != uf.find(idx2)) {
return false;
}
} else {
return false;
}
}
return true;
}
}
(2) Python
class Solution:
def areSentencesSimilarTwo(self, words1, words2, pairs):
"""
:type words1: List[str]
:type words2: List[str]
:type pairs: List[List[str]]
:rtype: bool
"""
if not words1 and not words2:
return True
elif not words1 or not words2:
return False
elif len(words1) != len(words2):
return False
def getKey(pair):
return pair[0]
pairSet = []
for pair in pairs:
pairSubSet = set(pair)
for i in range(len(pairSet)):
pe = pairSet.pop(0)
if pair[0] in pe or pair[1] in pe:
pairSubSet |= pe
else:
pairSet.append(pe)
pairSet.append(pairSubSet)
for i in range(len(words1)):
word1 = words1[i]
word2 = words2[i]
if word1 == word2:
continue
else:
found = False
for pairset in pairSet:
if word1 in pairset and word2 in pairset:
found = True
break
if found == False:
return False
return True
(3) Scala