LeetCode 734. Sentence Similarity
原题链接在这里: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
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 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;
}
}
LeetCode 734. Sentence Similarity的更多相关文章
- [LeetCode] 734. Sentence Similarity 句子相似度
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [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 ...
- 734. Sentence Similarity 有字典数组的相似句子
[抄题]: Given two sentences words1, words2 (each represented as an array of strings), and a list of si ...
- 【LeetCode】734. Sentence Similarity 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...
- LeetCode 737. Sentence Similarity II
原题链接在这里:https://leetcode.com/problems/sentence-similarity-ii/ 题目: Given two sentences words1, words2 ...
- [LeetCode] Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- Comparing Sentence Similarity Methods
Reference:Comparing Sentence Similarity Methods,知乎.
- 论文阅读笔记: Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks
论文概况 Multi-Perspective Sentence Similarity Modeling with Convolution Neural Networks是处理比较两个句子相似度的问题, ...
随机推荐
- LR编写grammar中的问题和解决方法
本文主要说明LR解析过程中关于BNF的典型冲突如何在LR中解决 冲突一般分为两种: shift/reduce错误 redure/redure错误 下面分别解释两种冲突 1. shift/reduce错 ...
- drools -规则语法
文章结构 1. 基础api 2. FACT对象 3. 规则 4. 函数 1. 基础api 在 Drools 当中,规则的编译与运行要通过Drools 提供的各种API 来实现,这些API 总体来讲可以 ...
- .Net Core 获取应用物理路径的常见问题
如果要得到传统的ASP.Net应用程序中的相对路径或虚拟路径对应的服务器物理路径,只需要使用使用Server.MapPath()方法来取得Asp.Net根目录的物理路径. 但是在Asp.Net Cor ...
- SQL server数据库创建代码,filegroup文件组修改,
以下示例在 SQL Server 实例上创建了一个数据库.该数据库包括一个主数据文件.一个用户定义文件组和一个日志文件.主数据文件在主文件组中,而用户定义文件组包含两个次要数据文件.ALTER DAT ...
- C#利用控件mscomm32.ocx读取串口datalogic扫描枪数据
1).开发环境VS12,语言C# 2).扫描枪品牌:datalogic 4470 3).通讯协议:串口 1.首先,第一步创建一个新工程,windows窗体应用程序,命名为TestScanner,如下: ...
- Vertx与Spring配合完成DML操作
服务启动: public static void main( String[] args ) { ApplicationContext context = new AnnotationConfigAp ...
- http的GET方法参数中不能传列表,接收端的key会变
如下 async initTable() { await getHostAttributesForUser({'username': this.username}).then(response =&g ...
- prometheus消耗内存问题
参考: https://stackoverflow.com/questions/56115912/why-does-prometheus-consume-so-much-memory https:// ...
- JS面向对象设计-理解对象
不同于其他面向对象语言(OO,Object-Oriented),JS的ECMAScript没有类的概念, 它把对象定义为"无序属性(基本值.对象.函数)的集合",类似于散列表. 每 ...
- React Children 使用
React 有一个特殊的属性children, 主要用于组件需要渲染内容,但它并不知道具体要渲染什么内容,怎么会有这种使用场景?确实比较少,但并不是没有,比如弹出框.当你写一个弹出框组件的时候,你知道 ...