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].

734. Sentence Similarity 的拓展。不同点,这题的相似单词可以传递。

解法1:DFS。本质是无向连通图的问题,把pairs数组中的每一对相似的字符串视为无向图上的两个结点,对每个结点要记录所有和其相连的结点,比如(a, b), (b, c)和(c, d)的映射关系:a -> {b},  b -> {a, c},  c -> {b, d},  d -> {c}。要验证a和d是否相似,从a只能找到b,b可以找到a、c, a访问过,将访问过的结点加入一个集合visited,此时只能访问c,c里面有b、d,找到d,说明a和d相似。

解法2:BFS。与 DFS 类似,只是写成迭代形式。

解法3:Union Find

Java:

class Solution {
public boolean areSentencesSimilarTwo(String[] words1, String[] words2, String[][] pairs) {
if (words1.length != words2.length) {
return false;
}
// Build the graph of pairs
HashMap<String, Set<String>> pairMap = new HashMap<>();
for (String[] pair : pairs) {
// Create keys(words in [][]pairs without duplication) and empty set
if (!pairMap.containsKey(pair[0])) {
pairMap.put(pair[0], new HashSet<String>());
}
if (!pairMap.containsKey(pair[1])) {
pairMap.put(pair[1], new HashSet<String>());
}
// Add the corresponding pairs to each other
pairMap.get(pair[0]).add(pair[1]);
pairMap.get(pair[1]).add(pair[0]);
} // Iterate throught each word in both input strings and do DFS search
for (int i = 0; i < words1.length; i++) {
// If same, then we don't need to do DFS search
if (words1[i].equals(words2[i])) continue;
// If they are not the same and no such strings in the pairs
if (!pairMap.containsKey(words1[i]) || !pairMap.containsKey(words2[i])) return false;
// Do DFS search, initialize the set to prevent revisiting.
if (!dfs(words1[i], words2[i], pairMap, new HashSet<>())) return false;
}
return true;
} public boolean dfs(String source, String target, HashMap<String, Set<String>> pairMap, HashSet<String> visited) {
if (pairMap.get(source).contains(target)) {
return true;
}
// Mark as visited
visited.add(source);
for (String next : pairMap.get(source)) {
// DFS other connected node, except the mirrowed string
if (!visited.contains(next) && next.equals(target) ||
!visited.contains(next) && dfs(next, target, pairMap, visited)) {
return true;
}
}
// We've done dfs still can't find the target
return false;
}
}  

Java: Union Find

class Solution {
public boolean areSentencesSimilarTwo(String[] words1, String[] words2, String[][] pairs) {
if(words1.length!=words2.length) return false;
Map<String, String> map = new HashMap<>();
for(String[] pair : pairs){
String parent0 = find(pair[0], map);
String parent1 = find(pair[1], map);
if(!parent0.equals(parent1)) map.put(parent0, parent1);
}
int n = words1.length;
for(int i=0; i<n; i++){
if (!words1[i].equals(words2[i]) && !find(words1[i], map).equals(find(words2[i], map))) return false;
}
return true;
} private String find(String word, Map<String, String> map){
if(!map.containsKey(word)) return word;
String str = word;
while(map.containsKey(str)){
str = map.get(str);
}
map.put(word, str);
return str;
}
}

Python: DFS

class Solution(object):
def areSentencesSimilarTwo(self, words1, words2, pairs):
"""
:type words1: List[str]
:type words2: List[str]
:type pairs: List[List[str]]
:rtype: bool
"""
if len(words1) != len(words2): return False
similars = collections.defaultdict(set)
for w1, w2 in pairs:
similars[w1].add(w2)
similars[w2].add(w1) def dfs(words1, words2, visits):
for similar in similars[words2]:
if words1 == similar:
return True
elif similar not in visits:
visits.add(similar)
if dfs(words1, similar, visits):
return True
return False for w1, w2 in zip(words1, words2):
if w1 != w2 and not dfs(w1, w2, set([w2])):
return False
return True  

Python:BFS

class Solution(object):
def areSentencesSimilarTwo(self, words1, words2, pairs):
if len(words1) != len(words2): return False
graph = collections.defaultdict(list)
for w1, w2 in pairs:
graph[w1].append(w2)
graph[w2].append(w1) for w1, w2 in zip(words1, words2):
stack, seen = [w1], {w1}
while stack:
word = stack.pop()
if word == w2: break
for nei in graph[word]:
if nei not in seen:
seen.add(nei)
stack.append(nei)
else:
return False
return True

Python: DSU(Disjoint Set Union)  

class DSU:
def __init__(self, N):
self.par = range(N)
def find(self, x):
if self.par[x] != x:
self.par[x] = self.find(self.par[x])
return self.par[x]
def union(self, x, y):
self.par[self.find(x)] = self.find(y) class Solution(object):
def areSentencesSimilarTwo(self, words1, words2, pairs):
if len(words1) != len(words2): return False index = {}
count = itertools.count()
dsu = DSU(2 * len(pairs))
for pair in pairs:
for p in pair:
if p not in index:
index[p] = next(count)
dsu.union(index[pair[0]], index[pair[1]]) return all(w1 == w2 or
w1 in index and w2 in index and
dsu.find(index[w1]) == dsu.find(index[w2])
for w1, w2 in zip(words1, words2))  

C++: DFS

class Solution {
public:
bool areSentencesSimilarTwo(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);
m[pair.second].insert(pair.first);
}
for (int i = 0; i < words1.size(); ++i) {
unordered_set<string> visited;
if (!helper(m, words1[i], words2[i], visited)) return false;
}
return true;
}
bool helper(unordered_map<string, unordered_set<string>>& m, string& cur, string& target, unordered_set<string>& visited) {
if (cur == target) return true;
visited.insert(cur);
for (string word : m[cur]) {
if (!visited.count(word) && helper(m, word, target, visited)) return true;
}
return false;
}
};

C++: BFS

class Solution {
public:
bool areSentencesSimilarTwo(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);
m[pair.second].insert(pair.first);
}
for (int i = 0; i < words1.size(); ++i) {
if (words1[i] == words2[i]) continue;
unordered_set<string> visited;
queue<string> q{{words1[i]}};
bool succ = false;
while (!q.empty()) {
auto t = q.front(); q.pop();
if (m[t].count(words2[i])) {
succ = true; break;
}
visited.insert(t);
for (auto a : m[t]) {
if (!visited.count(a)) q.push(a);
}
}
if (!succ) return false;
}
return true;
}
};

C++:  

class Solution {
public:
bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
if (words1.size() != words2.size()) return false;
unordered_map<string, string> m;
for (auto pair : pairs) {
string x = getRoot(pair.first, m), y = getRoot(pair.second, m);
if (x != y) m[x] = y;
}
for (int i = 0; i < words1.size(); ++i) {
if (getRoot(words1[i], m) != getRoot(words2[i], m)) return false;
}
return true;
}
string getRoot(string word, unordered_map<string, string>& m) {
if (!m.count(word)) m[word] = word;
return word == m[word] ? word : getRoot(m[word], m);
}
};

  

类似题目:

[LeetCode] 734. Sentence Similarity 句子相似度

[LeetCode] 547. Friend Circles 朋友圈

721. Accounts Merge  

All LeetCode Questions List 题目汇总

[LeetCode] 737. Sentence Similarity II 句子相似度 II的更多相关文章

  1. [LeetCode] 737. Sentence Similarity II 句子相似度之二

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  2. LeetCode 737. Sentence Similarity II

    原题链接在这里:https://leetcode.com/problems/sentence-similarity-ii/ 题目: Given two sentences words1, words2 ...

  3. [LeetCode] 734. Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  4. [LeetCode] Sentence Similarity II 句子相似度之二

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  5. LeetCode 734. Sentence Similarity

    原题链接在这里:https://leetcode.com/problems/sentence-similarity/ 题目: Given two sentences words1, words2 (e ...

  6. 734. Sentence Similarity 有字典数组的相似句子

    [抄题]: Given two sentences words1, words2 (each represented as an array of strings), and a list of si ...

  7. NLP入门(一)词袋模型及句子相似度

      本文作为笔者NLP入门系列文章第一篇,以后我们就要步入NLP时代.   本文将会介绍NLP中常见的词袋模型(Bag of Words)以及如何利用词袋模型来计算句子间的相似度(余弦相似度,cosi ...

  8. 论文阅读笔记: Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks

    论文概况 Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks是处理比较两个句子相似度的问题, ...

  9. Leetcode 137. 只出现一次的数字 II - 题解

    Leetcode 137. 只出现一次的数字 II - 题解 137. Single Number II 在线提交: https://leetcode.com/problems/single-numb ...

随机推荐

  1. 关于Discuz x3.3页面空白解决方法

    今天找时间分析了一下,找到了页面空白的原因,可能是因为php版本兼容性的问题所致,所以只是部分用户遇到这种情况,这里分享一下.经过分析发现是sourcefunctionfunction_core.ph ...

  2. 30、Python程序中的线程操作(oncurrent模块)

    进程是cpu资源分配的最小单元,一个进程中可以有多个线程. 线程是cpu计算的最小单元. 对于Python来说他的进程和线程和其他语言有差异,是有GIL锁. GIL锁 GIL锁保证一个进程中同一时刻只 ...

  3. HTML实现调用百度在线翻译API

    HTML实现调用百度在线翻译API 本文章已收录于:   <!doctype html> <html lang="en"> <head> < ...

  4. MySQL利用LOCATE,CONCAT查询

    SELECT * FROM table t WHERE 1=1 AND LOCATE(#{searchParams},CONCAT(t.account,IFNULL(t.id,''),IFNULL(t ...

  5. Spring cloud stream【消息分组】

      上篇文章我们简单的介绍了stream的使用,发现使用还是蛮方便的,但是在上个案例中,如果有多个消息接收者,那么消息生产者发送的消息会被多个消费者都接收到,这种情况在某些实际场景下是有很大问题的,比 ...

  6. 2019阿里JVM组实习面经

    面试质量非常高....非常高...高... 一面 自我介绍 看过hotspot哪些模块,模板解释器工作说一下,生成的native code放在哪,怎么处理safepoint的 说项目,实现了哪些字节码 ...

  7. LeetCode 286. Walls and Gates

    原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...

  8. 抽样方法(Sampling Method)

    概率抽样方法: 1. 随机抽样(random sampling):从有限总体中简单随机抽样或从无限总体中随机抽样. 具体实现方式:a. 抽签法:b. 随机数字法 2. 分层抽样(stratified ...

  9. Vue的Key属性,v-for和v-if,v-if/v-show,v-pre不渲染,v-once只渲染一次

    key属性为什么要加 key -- api 解释 key的特殊属性主要用在vue的虚拟dom算法,如果不适用key,vue会使用一种最大限度减少动态元素并且尽可能的尝试修复/再利用相同类型元素的算法. ...

  10. 重置GPU显存 Reset GPU memory after CUDA errors

    Sometimes CUDA program crashed during execution, before memory was flushed. As a result, device memo ...