[LeetCode] 737. Sentence Similarity II 句子相似度 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].
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的更多相关文章
- [LeetCode] 737. Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- LeetCode 737. Sentence Similarity II
原题链接在这里:https://leetcode.com/problems/sentence-similarity-ii/ 题目: Given two sentences words1, words2 ...
- [LeetCode] 734. Sentence Similarity 句子相似度
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [LeetCode] Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- LeetCode 734. Sentence Similarity
原题链接在这里:https://leetcode.com/problems/sentence-similarity/ 题目: Given two sentences words1, words2 (e ...
- 734. Sentence Similarity 有字典数组的相似句子
[抄题]: Given two sentences words1, words2 (each represented as an array of strings), and a list of si ...
- NLP入门(一)词袋模型及句子相似度
本文作为笔者NLP入门系列文章第一篇,以后我们就要步入NLP时代. 本文将会介绍NLP中常见的词袋模型(Bag of Words)以及如何利用词袋模型来计算句子间的相似度(余弦相似度,cosi ...
- 论文阅读笔记: Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks
论文概况 Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks是处理比较两个句子相似度的问题, ...
- Leetcode 137. 只出现一次的数字 II - 题解
Leetcode 137. 只出现一次的数字 II - 题解 137. Single Number II 在线提交: https://leetcode.com/problems/single-numb ...
随机推荐
- pycharm连不上数据库:报警代码Connection to api@localhost failed. [08001] Could not create connection to d
pycharm 换成2019之后连接数据库用户名密码数据库名字都没错,就是连接不上去,网上百度一下,试试将URL后面拼接 ?useSSL=false&serverTimezone=UTC 发现 ...
- 23、matplotlib数据可视化、绘图库模块
matplotlib官方文档:https://matplotlib.org/contents.html?v=20190307135750 matplotlib是一个绘图库,它可以创建常用的统计图,包括 ...
- yugabyte与cockroachdb 的几个区别
下图是来自官方文档 说明 今天打算尝试使用yugabyte做为hasura graphql-engine 的pg 引擎,发现比较完美,仔细看官方文档,原来yugabyte 底层实现直接是基于原生pg ...
- vector的使用注意事项
示例1: #include "iostream" #include "vector" using namespace std; int main(void) { ...
- Ubuntu下python3安装tkinter包
case1: 首先sudo apt-get update(如果不更新很有可能找不到tkinter),然后sudo apt-get install python3-tk,安装完成后就可以使用了. cas ...
- cf1173 D. Nauuo and Circle
链接 [cf]http://codeforces.com/contest/1175/problem/F) 思路 当1在1的位置做dp[i]为i的子树所有的方案. 一条性质是i的子树所占圆上的位置一定一 ...
- 【13NOIP提高组】转圈游戏(信息学奥赛一本通 1875)(洛谷 1965)
题目描述 nn 个小描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号 ...
- SpringBoot2.0给所有Controller添加统一前缀
需求 给所有Controller添加统一前缀 实现 在配置文件中添加:server.servlet.context-path: /api(不同SpringBoot版本会有区别,这里是采用2.0) 结果 ...
- shell 单引号以及双引号
#!/bin/bash a= echo '$a' echo "$a" your_name='runoob' str="Hello, I know you are \&qu ...
- 自定义的操作Cookie的工具类
可以在SpringMVC等环境中使用的操作Cookie的工具类 package utils; import java.io.UnsupportedEncodingException; import j ...