Skip to content

Instantly share code, notes, and snippets.

@aquawj
Last active December 20, 2017 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aquawj/1d8b62533b7a5d6633013cd0d5a47a43 to your computer and use it in GitHub Desktop.
Save aquawj/1d8b62533b7a5d6633013cd0d5a47a43 to your computer and use it in GitHub Desktop.
734. I:
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, "great acting skills" and "fine drama talent" are similar, if the similar word pairs are pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]].
Note that the similarity relation is not transitive. For example, if "great" and "fine" are similar, and "fine" and "good" are similar, "great" and "good" are not necessarily similar.
However, similarity is 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].
//hashmap<String, Set<String>> map: 存<单词,该单词所有同义词集合>
//对句子的每一个单词check,如果句1和句2在同一位置的单词本身不同,或者彼此不是同义词,return false
class Solution {
public boolean areSentencesSimilar(String[] a, String[] b, String[][] pairs) {
if (a.length != b.length) return false;
Map<String, Set<String>> map = new HashMap<>();
for (String[] p : pairs) {
if (!map.containsKey(p[0])) map.put(p[0], new HashSet<>());
map.get(p[0]).add(p[1]);
}
for (int i = 0; i < a.length; i++)
if (!a[i].equals(b[i]) && !map.getOrDefault(a[i], new HashSet<>()).contains(b[i]) && !map.getOrDefault(b[i], new HashSet<>()).contains(a[i]))
return false;
return true;
}
}
737. 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].
//可传递:Union Find
class Solution {
public boolean areSentencesSimilarTwo(String[] a, String[] b, String[][] pairs) {
if (a.length != b.length) return false;
Map<String, String> m = new HashMap<>();
for (String[] p : pairs) {
String parent1 = find(m, p[0]), parent2 = find(m, p[1]);
if (!parent1.equals(parent2)) m.put(parent1, parent2);
}
for (int i = 0; i < a.length; i++)
if (!a[i].equals(b[i]) && !find(m, a[i]).equals(find(m, b[i]))) return false;
return true;
}
private String find(Map<String, String> m, String s) {
if (!m.containsKey(s)) m.put(s, s);
return s.equals(m.get(s)) ? s : find(m, m.get(s));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment