原题链接在这里: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 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].

题解:

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的更多相关文章

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

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

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

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

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

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

  4. LeetCode 734. Sentence Similarity

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

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

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

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

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

  7. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  8. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  9. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

随机推荐

  1. yum 多线程插件,apt多线程插件

    1.安装yum-axelget插件,默认 yum使用单线程下载,安装该插件后,会使用多线程下载. yum -y install yum-axelget 2. apt-fast安装 https://gi ...

  2. 解决source insight 4.0 不识别.cc文件的问题

    Options -> File Type Options, File Filter 中加入,*.cc 参考了C++ Primer Plus第五版中文版 P8 C++实现 源代码的扩展名 UNIX ...

  3. Java学习:Debug调试程序

    Debug追踪 Debug调试程序: 可以让代码逐行执行,查看代码执行的过程,调试程序中出现的bug 使用方式: 在行号的右边,鼠标左键单击,添加断点(每个方法的第一行,哪里有bug添加到哪里) 右键 ...

  4. IEDA 启动main报 stock

    1 . 启动资源管理器关闭java进程,重新启动还是没有解决问题 2.看网上说jdk版本1.8换成1.7 启动成功,已解决,不知道为为什么.

  5. ASP.NET Core中app.UseDeveloperExceptionPage和app.UseExceptionHandler方法有什么用

    在新建一个ASP.NET Core项目后,在项目Startup类的Configure方法中默认会添加两个方法的调用,app.UseDeveloperExceptionPage和app.UseExcep ...

  6. C#DataTable使用方法详解

    在项目中常常常使用到DataTable,假设DataTable使用得当,不仅能使程序简洁有用,并且可以提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 1.添加引用 1 2 ...

  7. python中用分别用selenium、requests库实现Windows认证登录

    最近在搞单位的项目,实现python自动化,结果在第一步就把我给拒之门外,查资料问大佬,问我们开发人员,从周一折腾到周五才搞定了 接下给大家分享一下 项目背景:我们系统是基于Windows平台实现的, ...

  8. ASP.NET SignalR 系列(七)之服务端触发推送

    前面几章讲的都是从客户端触发信息推送的,但在实际项目中,很多信息可能是由系统服务端推送的,下面2图分别展示两种通道 客户端触发推送 服务端推送 下面我们就重点介绍下服务端如何调用集线器的对象进行推送 ...

  9. Node.js 连接 MongoDB数据库

    安装指令:npm install mongodb var mongodb = require("mongodb");// console.log(mongodb); var Mon ...

  10. Beego 学习笔记10:Easyui使用

    EasyUI使用 1>     下载EasyUI.下载地址:http://www.jeasyui.com/download/index.php 根据自己使用的是jquery还是Angular进行 ...