737. Sentence Similarity II
来源:互联网 发布:mac版360浏览器 编辑:程序博客网 时间:2024/05/21 23:25
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”].
其实这题本质就是建立一个无向图,跟找连通分量是一回事。将pairs里出现的每一个单词当作一个点,每一个组合当作一条线,只要两个单词之间可以有路径形成,就是一个近义词,也就是两个单词处于一个连通分量里。所以深度优先搜索就行了。
方法一:递归写法
class Solution {public: bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) { int len = words1.size(); if (words2.size() != len) return false; unordered_map<string, vector<string>> hash; unordered_set<string> isVisited; for (auto p : pairs) { hash[p.first].push_back(p.second); hash[p.second].push_back(p.first); } for (int i = 0; i < len; i++) { if (words1[i] == words2[i]) continue; isVisited.clear(); if (DFS(words1[i], words2[i], hash, isVisited)) continue; return false; } return true; } bool DFS(string s1, string s2, unordered_map<string, vector<string>>& hash, unordered_set<string>& isVisited) { if (isVisited.find(s1) == isVisited.end()) { isVisited.emplace(s1); for (auto s : hash[s1]) { if (s2 == s) return true; if (DFS(s, s2, hash, isVisited)) return true; } return false; } return false; }};
方法二:非递归写法
class Solution {public: bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) { int len = words1.size(); if (words2.size() != len) return false; unordered_map<string, vector<string>> hash; unordered_set<string> isVisited; for (auto p : pairs) { hash[p.first].push_back(p.second); hash[p.second].push_back(p.first); } for (int i = 0; i < len; i++) { if (words1[i] == words2[i]) continue; isVisited.clear(); stack<string> st; st.push(words1[i]); isVisited.emplace(words1[i]); bool flag = false; while (!st.empty() && flag == false) { string w1 = st.top(); st.pop(); for (auto s : hash[w1]) { if (isVisited.find(s) == isVisited.end()) { if (s == words2[i]) { flag = true; break; } isVisited.emplace(s); st.push(s); } } } if (flag == false) return false; } return true; }};
- 737. Sentence Similarity II
- 737. Sentence Similarity II
- 737. Sentence Similarity II
- LeetCode # 737 Sentence Similarity II
- Sentence Similarity II问题及解法
- Sentence Similarity
- Leetcode算法学习日志-737 Sentence Similarity II
- 734. Sentence Similarity
- 734. Sentence Similarity
- 734. Sentence Similarity
- LeetCode | 734. Sentence Similarity
- Sentence Similarity问题及解法
- LWC 60:734. Sentence Similarity
- sentence
- Sentence
- SQL sentence!
- everyday sentence
- userful sentence
- Centos7安装Mysql5.7
- 东方耀 手把手教React Native实战开发视频教程共237集
- a
- Spring生命周期中常用的Bean扩展接口
- 用scala 写spark程序
- 737. Sentence Similarity II
- buildroot工作原理(第14zhan)
- Map中提供的一个算法(提供一个值,返回大于等于该值的最接近的2的指数幂)
- 讲解交叉验证函数较清晰的一篇博文
- java多线程之-线程创建方式
- 处理 Windows 虚拟机的计划内维护通知
- 纯CSS3动画之旋转的立方体
- springCloud 配置中心细节
- 有关JSP