原题链接在这里:https://leetcode.com/problems/sentence-similarity/

题目:

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, "great acting skills" and "fine drama talent" are similar, if the similar word pairs are pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]].

Note that the similarity relation is not transitive. For example, if "great" and "fine" are similar, and "fine" and "good" are similar, "great" and "good" are not necessarily similar.

However, similarity is 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 maintain symmetric, check the word combination left ^ right, and right ^ left to see if it is in the set.

Time Complexity: O(m+n). m = words1.length. n = pairs.size().

Space: O(n).

AC Java:

 class Solution {
public boolean areSentencesSimilar(String[] words1, String[] words2, List<List<String>> pairs) {
if(words1 == null && words2 == null){
return true;
} if(words1 == null || words2 == null || words1.length != words2.length){
return false;
} HashSet<String> hs = new HashSet<String>();
for(List<String> pair : pairs){
hs.add(pair.get(0) + "^" + pair.get(1));
} for(int i = 0; i<words1.length; i++){
if(words1[i].equals(words2[i]) ||
hs.contains(words1[i]+"^"+words2[i]) ||
hs.contains(words2[i]+"^"+words1[i])){
continue;
} return false;
} return true;
}
}

类似Sentence Similarity II.

LeetCode 734. Sentence Similarity的更多相关文章

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

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

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

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

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

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

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

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

  5. 【LeetCode】734. Sentence Similarity 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...

  6. LeetCode 737. Sentence Similarity II

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

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

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

  8. Comparing Sentence Similarity Methods

    Reference:Comparing Sentence Similarity Methods,知乎.

  9. 论文阅读笔记: Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks

    论文概况 Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks是处理比较两个句子相似度的问题, ...

随机推荐

  1. 65 TCP连接中,流的关闭会造成Socket的关闭

    转自:https://blog.csdn.net/u012525096/article/details/76924627 今天写安卓向服务器发送图片,过程为:客户端发送数据->服务器接收.处理数 ...

  2. 7. Scala面向对象编程(中级部分)

    7.1 包 7.1.1 看一个应用场景 现在有两个程序员共同开发一个项目,程序员xiaoming希望定义一个类取名Dog,程序员xiaohong也想定一个类也叫Dog,两个程序员还为此吵了起来,该怎么 ...

  3. SQL分类之DML:增删改表中的数据

    DML:增删改表中的数据 1.添加数据: 语法: insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n): 注意: 1.列名和值要一一对应. 2.如果表名 ...

  4. mongodb数据库怎么迁移备份?

    1.先进入到mongodb目录的bin目录下,Linux windos是一样   image.png 2.集合备份和恢复 使用mongo自带命令来迁移数据,思路是先导出集合数据再导入到数据库中 导出命 ...

  5. os.mkdir()与 shutil.rmtree()对文件夹的 创建与删除

    import osimport shutil # os.mkdir('C:/Users/Desktop/123') # 表示在桌面上创建文件# os.mkdir('123') # 表示在此代码文件下创 ...

  6. SpringCloud-Consul开发环境配置

    一.consul安装 1.下载:https://www.consul.io/downloads.html: 2.选择版本:本人开发环境是windows,所以选择win64: 3.安装:保存至D:/Sp ...

  7. BUAA-OO-2019 第二单元总结

    第五次作业 本次作业,需要完成的任务为单部多线程傻瓜调度(FAFS)电梯的模拟. 设计策略 先来先服务的单电梯是一个标准的"生产者-消费者"模型.虽然在本次作业中调度器似乎是不必要 ...

  8. CSS中:和::

    一个冒号是伪类,两个冒号是伪元素 伪类可以独立于文档的元素来分配样式,且可以分配给任何元素,逻辑上和功能上类类似,但是其是预定义的.不存在于文档树中且表达方式也不同,所以叫伪类.伪元素所控制的内容和一 ...

  9. css3中的calc的使用

    最近在布局的时候遇到一个问题,在页面中的左侧是侧边栏,右边是内容区域,内容区域中有一个固定定位的标签页,在设置固定定位的标签设置宽度的时候应该是内容区域的宽度,而固定定位的时候相对于是窗口的宽度,所以 ...

  10. Android-----解析xml文件的三种方式

    SAX解析方法介绍: SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备.SAX解析XML文件采用的是事件驱动,也就是说, ...