Related: LeetCode 737 - Sentence Similarity II
http://zxi.mytechroad.com/blog/hashtable/leetcode-734-sentence-similarity/
X.
http://www.cnblogs.com/grandyang/p/8016251.html
我们可以根据pairs中的内容,建立起字符串到字符串之间的映射,不过需要注意到一个字符串可能会有多个字符串和它类似。为了减少存储空间,我在建立映射的过程中,约定将大的字符串映射到小的字符串中。
当有了字典之后,我们就依次遍历words1和words2中的对应字符串,看看他们的相似关系是否可以在字典中找到。如果找不到就提前返回false,否则最后返回true。
bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
if (words1.size() != words2.size()) {
return false;
}
unordered_map<string, set<string>> hash; // map from big word to small word
for (auto it = pairs.begin(); it != pairs.end(); ++it) {
if (it->first < it->second) {
hash[it->second].insert(it->first);
}
else {
hash[it->first].insert(it->second);
}
}
for (int i = 0; i < words1.size(); ++i) {
if (words1[i] < words2[i]) {
if (hash.count(words2[i]) == 0 || hash[words2[i]].find(words1[i]) == hash[words2[i]].end()) {
return false;
}
}
else if (words1[i] > words2[i]) {
if (hash.count(words1[i]) == 0 || hash[words1[i]].find(words2[i]) == hash[words1[i]].end()) {
return false;
}
}
}
return true;
}
http://zxi.mytechroad.com/blog/hashtable/leetcode-734-sentence-similarity/
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
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]
.
Use hashtable to store mapping from word to its similar words.
public boolean areSentencesSimilar(String[] words1, String[] words2, String[][] pairs) {
if (words1.length != words2.length) return false;
Map<String, Set<String>> similar_words = new HashMap<>();
for (String[] pair : pairs) {
if (!similar_words.containsKey(pair[0]))
similar_words.put(pair[0], new HashSet<>());
if (!similar_words.containsKey(pair[1]))
similar_words.put(pair[1], new HashSet<>());
similar_words.get(pair[0]).add(pair[1]);
similar_words.get(pair[1]).add(pair[0]);
}
for (int i = 0; i < words1.length; ++i) {
if (words1[i].equals(words2[i])) continue;
if (!similar_words.containsKey(words1[i])) return false;
if (!similar_words.get(words1[i]).contains(words2[i])) return false;
}
return true;
}
First of all, we need to make sure we have considered of all the edge cases in this question. So there are 3 conditions for a word:
- There is no similar pair exists for this word.
- There is one pair exists for this word.
- There are more than one pair exists for this word.
Considering about all the above conditions, we need to use a one to more relationship when storing the pair relationship. This can be realized via a
HashMap<String, HashSet<String>>
for quick (O(1)
) search for a word's similar words.
And based on the question, we have two conditions when the two input arrays are not similar:
- The two input array are of different length.
- For a word at position
i
inwords[]
, if this word does not exists in the HashMap or the HashSet corresponding to it does not containswords2[i]
. Meanwhile,words1[i] != words2[i]
.
Time complexity:
O(n)
where n
is the greater one among words1.length
and pairs.length
.
Space complexity:
O(2*m) = O(m)
where m
is the length of pairs
.public boolean areSentencesSimilar(String[] words1, String[] words2, String[][] pairs) { if (words1.length != words2.length) return false; Map<String, Set<String>> map = new HashMap<>(); for (String[] pair : pairs) { if (!map.containsKey(pair[0])) { map.put(pair[0], new HashSet<>()); } if (!map.containsKey(pair[1])) { map.put(pair[1], new HashSet<>()); } map.get(pair[0]).add(pair[1]); map.get(pair[1]).add(pair[0]); } for (int i = 0; i < words1.length; i++) { if (!words1[i].equals(words2[i]) && (!map.containsKey(words1[i]) || !map.get(words1[i]).contains(words2[i]))) { return false; } // if (words1[i].equals(words2[i])) continue; // if (map.containsKey(words1[i]) && map.get(words1[i]).contains(words2[i])) continue; // return false; } return true; }
X.
http://www.cnblogs.com/grandyang/p/8016251.html
这道题给了我们两个句子,问这两个句子是否是相似的。判定的条件是两个句子的单词数要相同,而且每两个对应的单词要是相似度,这里会给一些相似的单词对,这里说明了单词对的相似具有互逆性但是没有传递性。看到这里博主似乎已经看到了Follow up了,加上传递性就是一个很好的拓展。那么这里没有传递性,就使得问题变得很容易了,我们只要建立一个单词和其所有相似单词的集合的映射就可以了,比如说如果great和fine类似,且great和good类似,那么就有下面这个映射:
great -> {fine, good}
所以我们在逐个检验两个句子中对应的单词时就可以直接去映射中找,注意有可能遇到的单词对时反过来的,比如fine和great,所以我们两个单词都要带到映射中去查找,只要有一个能查找到,就说明是相似的,反之,如果两个都没查找到,说明不相似,直接返回false
bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) { if (words1.size() != words2.size()) return false; unordered_map<string, unordered_set<string>> m; for (auto pair : pairs) { m[pair.first].insert(pair.second); } for (int i = 0; i < words1.size(); ++i) { if (words1[i] == words2[i]) continue; if (!m[words1[i]].count(words2[i]) && !m[words2[i]].count(words1[i])) return false; } return true; }https://blog.csdn.net/magicbean2/article/details/79410076
我们可以根据pairs中的内容,建立起字符串到字符串之间的映射,不过需要注意到一个字符串可能会有多个字符串和它类似。为了减少存储空间,我在建立映射的过程中,约定将大的字符串映射到小的字符串中。
当有了字典之后,我们就依次遍历words1和words2中的对应字符串,看看他们的相似关系是否可以在字典中找到。如果找不到就提前返回false,否则最后返回true。
bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
if (words1.size() != words2.size()) {
return false;
}
unordered_map<string, set<string>> hash; // map from big word to small word
for (auto it = pairs.begin(); it != pairs.end(); ++it) {
if (it->first < it->second) {
hash[it->second].insert(it->first);
}
else {
hash[it->first].insert(it->second);
}
}
for (int i = 0; i < words1.size(); ++i) {
if (words1[i] < words2[i]) {
if (hash.count(words2[i]) == 0 || hash[words2[i]].find(words1[i]) == hash[words2[i]].end()) {
return false;
}
}
else if (words1[i] > words2[i]) {
if (hash.count(words1[i]) == 0 || hash[words1[i]].find(words2[i]) == hash[words1[i]].end()) {
return false;
}
}
}
return true;
}