LeetCode 737. Sentence Similarity II
原题链接在这里:https://leetcode.com/problems/sentence-similarity-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
words1andwords2will not exceed1000. - The length of
pairswill 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].
题解:
In order to fulfill transitive, we could use Union-Find.
For each pair, find both ancestor, and have one as parent of the other.
Then when comparing the words1 and words2, if both word are neight equal nor having the same ancestor, then it is not similar, return false.
Time Complexity: O((m+n)*logm). m = pairs.size(). n = words1.length. find takes O(logm). With path comparison and union by rank, it takes amatorize O(1).
Space: O(m).
AC Java:
class Solution {
public boolean areSentencesSimilarTwo(String[] words1, String[] words2, List<List<String>> pairs) {
if(words1.length != words2.length){
return false;
}
HashMap<String, String> hm = new HashMap<>();
for(List<String> pair : pairs){
String p1 = find(hm, pair.get(0));
String p2 = find(hm, pair.get(1));
hm.put(p1, p2);
}
for(int i = 0; i<words1.length; i++){
if(!words1[i].equals(words2[i]) && !find(hm, words1[i]).equals(find(hm, words2[i]))){
return false;
}
}
return true;
}
private String find(HashMap<String, String> hm, String s){
hm.putIfAbsent(s, s);
return s.equals(hm.get(s)) ? s : find(hm, hm.get(s));
}
}
LeetCode 737. Sentence Similarity II的更多相关文章
- [LeetCode] 737. Sentence Similarity II 句子相似度 II
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [LeetCode] 737. Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [LeetCode] 734. Sentence Similarity 句子相似度
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 ...
- [LeetCode] Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- 734. Sentence Similarity 有字典数组的相似句子
[抄题]: Given two sentences words1, words2 (each represented as an array of strings), and a list of si ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
随机推荐
- 【chromium】 cef源码下载
至少需要17GB的磁盘空间,不光有CEF源码,还会下载chromium源码.编译master分支的话,如果编译到chromium可能会需要windows sdk,windows sdk的版本可以参考下 ...
- C# Winform控件字体大小自适应
using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace WGClie ...
- 7、VUE事件
1.事件处理 Vue.js使用v-on指令监听DOM事件来触发JS回调函数. V-on: 缩写为 @ 事件回调函数可以传入$event这个事件对象. 2.事件修饰符 在事件处理程序中调用event.p ...
- 安装fastnlp
直接 pip install fastnlp 如果出现 torch的安装报错,可能与操作系统及 CUDA 的版本相关.直接上pytorch 的官网 https://pytorch.org/get-st ...
- Matlab访问者模式
访问者(Visitor)模式的定义:将作用于某种数据结构中的各元素的操作分离出来封装成独立的类,使其在不改变数据结构的前提下可以添加作用于这些元素的新的操作,为数据结构中的每个元素提供多种访问方式.它 ...
- js查询手机号码格式是否正确
直接上代码,复制开用 let regExp = /^([-]{,}\-)?[-]{,}$|^?[|||7||][-]\d{}$/; //验证的手机号码格式 //this.ruleForm.adminC ...
- 英语gzibeads天珠gzibeads单词
天珠英语是gZiBeads,藏语叫(si , 斯)汉语译为“斯”或“瑟”,又称“天降石”.在<藏汉大辞典>里天珠的解释为:“亚玛瑙,猫睛石,一种宝石,俗称九眼珠.入药能治脑溢血”.最早的天 ...
- WDA演练二:主界面设计(一)
前面已经完成了登陆界面的开发,下面就是主页面的展示了. 一.新建菜单表: 这里说明一下,考虑到简单点,这里只用了两级菜单展示,表里在配置的时候也指挥有一级,二级. AUGRP对应前面用户表的GROUP ...
- 浅谈Python设计模式 - 适配器模式
声明:本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 从本篇便开始介绍结构型设计模式,而适配器设计模式便是该类设计模式的一种,那么什么 ...
- pip问题:ImportError: cannot import name main
问题描述 今天使用pip安装python包的时候,提示可以升级到最新版的pip,然后就升级了pip,从8.1.1到19.0.3,结果,就出现了下面的问题,pip不能用了: Traceback (mos ...