原题链接在这里: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));
}
}

最新文章

  1. HTML5 Canvas绘制转盘抽奖
  2. 匈牙利 算法&amp;模板
  3. Apache Kafka - Quick Start on Windows
  4. JNI 概述【转】
  5. Android笔记——Drawerlayout创建侧滑出菜单
  6. Delphi 停靠技术的应用
  7. 7.3.1 Establishing a Backup Policy
  8. 静态链表实现 (A-B)U(B-A)
  9. centos5.5 安装git
  10. ☆ [HNOI2012] 永无乡 「平衡树启发式合并」
  11. GenomicConsensus (quiver, arrow)使用方法 | 序列 consensus
  12. Jmeter-接口功能测试
  13. Google 镜像站搜集
  14. 【mybatis】认识selectKey
  15. streamsets origin 说明
  16. c#后台读写Cookie
  17. web.xml中Listener的作用
  18. hdu 1425 Happy 2004
  19. HDU 5402 Travelling Salesman Problem(多校9 模拟)
  20. C++(变量类型-深入)

热门文章

  1. ETCD 添加节点报错 tocommit(2314438) is out of range [lastIndex(0)]. Was the raft log corrupted, truncated, or lost?
  2. Java的内存需要划分成为5个部分:
  3. 基于netty手写RPC框架
  4. JavaScript由来
  5. CentOS 7.0 更改SSH 远程连接 端口号
  6. 手机端 关闭当前页面的JS
  7. [K8s 1.9实践]Kubeadm 1.9 HA 高可用 集群 本地离线镜像部署
  8. String常用使用方法,1.创建string的常用3+1种方式,2.引用类型使用==比较地址值,3.String当中获取相关的常用方法,4.字符串的截取方法,5.String转换常用方法,6.切割字符串----java
  9. English--并列句
  10. Nginx 开启status用以监控状态信息