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)
随机推荐
- memcached源码分析一-slab
Slab作为一种内存管理方案,其作用主要有以下2点: a) 避免频繁的内存分配释放造成的内存碎片 b) 减少内存分配操作产生的性能开销 Linux内核数据结构中也有slab的设计,Linux提供了一套 ...
- golang知识精要(一)
一.第一章 命令行参数可通过os.Args访问,os.Args是切片 切片遵循左闭右开原则,如sl[1:3]不包含下标为3的元素 for循环两种方式 方式一: for initial; conditi ...
- 初步学习async/await,Task.GetAwaiter,Task.Result
网上关于async/await的知识有很多,看了很多但不如自己实践一遍来得快,所以这里记录下我的理解和大家学习下. 首先以最简单的同步方法来开始如下 private static void Test( ...
- mybatis映射mapper文件做like模糊查询
方法:使用concat函数连接通配符
- 对 GAN 的 value function 的理解
上式分为两个步骤: 第一步:调整discriminative model D的权重,使得V中两项取得最大值 第二步:调整generative model G的权重,使得V中第二项取得最小值 首先,分析 ...
- Macaca app inspector-ios真机设备UI查看器
前言: App Inspector:浏览器端的移动设备 UI 查看器,使用树状态结构查看 UI 布局,自动生成 XPaths.官网:https://macacajs.github.io/app-ins ...
- ERP会计科目表初始化
2019会计科目表 一.资产类 顺序号 编号 会计科目名称 会计科目适用范围 顺序号 编号 会计科目名称 会计科目适用范围 1 1001 库存现金 38 1431 周转材料 建造承包商专用 2 100 ...
- 大数据技术之Sqoop
大数据技术之Sqoop 一.Sqoop简介 Apache Sqoop(TM)是一种旨在有效地在Apache Hadoop和诸如关系数据库等结构化数据存储之间传输大量数据的工具. Sqoop于2012 ...
- Java开发环境配置大全
Java开发环境配置 零章:JDK安装教程 壹章:Tomcat安装教程 贰章:IntelliJ IDEA安装教程 叁章:MySql安装教程 肆章:Maven安装教程 伍章:MongoDB安装教程 陆章 ...
- 浅谈Python设计模式 - 建造者模式
声明:本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 建造者模式 当我们想要创建一个由多个部分构成的对象,而且他们的构建需要一步接一步 ...