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
andwords2
will not exceed1000
.* The length ofpairs
will not exceed2000
.* The length of eachpairs[i]
will be2
.* The length of eachwords[i]
andpairs[i][j]
will be in the range[1, 20]
.
Solution
Union-Find, O(n + k), S(n)
n: pairs.length
k: word1.length()
这道题的思路跟account merge很像,都是先构建一张无向图,执行Union Find,最后比较。
class Solution {
public boolean areSentencesSimilarTwo(String[] w1, String[] w2, String[][] pairs) {
if (w1 == null || w2 == null || w1.length != w2.length) {
return false;
}
int id = 0; // assign a unique id to each unique word
Map<String, Integer> word2Id = new HashMap<>();
UnionFind uf = new UnionFind(pairs.length * 2); // large enough
for (String[] pair : pairs) {
for (String p : pair) {
if (!word2Id.containsKey(p)) {
word2Id.put(p, id++);
}
}
uf.union(word2Id.get(pair[0]), word2Id.get(pair[1]));
}
for (int i = 0; i < w1.length; ++i) {
if (w1[i].equals(w2[i])
|| (word2Id.containsKey(w1[i]) && word2Id.containsKey(w2[i])
&& uf.isConnected(word2Id.get(w1[i]), word2Id.get(w2[i])))) {
continue;
}
return false;
}
return true;
}
class UnionFind {
private int[] parent;
public UnionFind(int n) {
parent = new int[n];
Arrays.fill(parent, -1);
}
public int find(int i) {
if (parent[i] == -1) {
return i;
}
parent[i] = find(parent[i]);
return parent[i];
}
public void union(int i, int j) {
int iset = find(i);
int jset = find(j);
if (iset != jset) {
parent[iset] = jset;
}
}
public boolean isConnected(int i, int j) {
return find(i) == find(j);
}
}
}
或者define Node class,graph中的每个点是一个Node:
class Solution {
public boolean areSentencesSimilarTwo(String[] words1, String[] words2, String[][] pairs) {
if (words1 == null || words2 == null || words1.length != words2.length || pairs == null) {
return false;
}
Map<String, Node> graph = new HashMap<>();
for (String[] pair : pairs) {
String a = pair[0];
String b = pair[1];
if (!graph.containsKey(a)) {
graph.put(a, new Node());
}
if (!graph.containsKey(b)) {
graph.put(b, new Node());
}
union(graph.get(a), graph.get(b));
}
for (int i = 0; i < words1.length; ++i) {
String s1 = words1[i];
String s2 = words2[i];
if (s1.equals(s2) || (graph.containsKey(s1) && graph.containsKey(s2)
&& isConnected(graph.get(s1), graph.get(s2)))) {
continue;
}
return false;
}
return true;
}
private boolean isConnected(Node p, Node q) {
return find(p) == find(q);
}
private Node find(Node p) {
if (p.parent == null) {
return p;
}
p.parent = find(p.parent);
return p.parent;
}
private void union(Node p, Node q) {
if (isConnected(p, q)) {
return;
}
Node pRoot = find(p);
Node qRoot = find(q);
pRoot.parent = qRoot;
}
class Node {
Node parent;
}
}