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是处理比较两个句子相似度的问题, ...
随机推荐
- SpringBoot中使用@scheduled定时执行任务需要注意的坑
spring boot: 计划任务@ EnableScheduling和@Scheduled @Scheduled中的参数说明 @Scheduled(fixedRate=2000):上一次开始执行时间 ...
- C# Winform 只允许输入数字
if (!(e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.')) e.Handled = true; if ...
- CLR学习之初识CLR
一.什么是CLR? CLR即公共语言运行时(Common Language Runtime,简称CRL),就是微软为.net产品构建的运行环境,与java的JVM类似,通俗的讲就是.net虚拟机.CL ...
- 类再生(合成、继承、final)
类再生 有两种方法达到代码复用的效果:合成.继承. 合成的语法 合成就是形成对象,把复用的代码置入对象句柄. 在类内字段使用基本数据会初始化为零,但对象句柄会初始化为null.在下面的程序中若没有ne ...
- MySQL之简介以及数据类型(一)
一:关系型数据库 所谓的关系型数据库RDBMS,是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据. 二:关系型数据库的主要产品: oracle:在以前的大型项目中使用 ...
- Java自学-接口与继承 抽象类
Java 抽象类 在类中声明一个方法,这个方法没有实现体,是一个"空"方法 这样的方法就叫抽象方法,使用修饰符"abstract" 当一个类有抽象方法的时候,该 ...
- 文件转base64处理或转换blob对象链接
一.文件转base64,代码: axios({ method: 'get', url: apiPath.common.downloaddUrl, responseType: 'blob'}).then ...
- js中 json对象与json字符串相互转换的几种方式
以下总结js中 json对象与json字符串相互转换的几种方式: 一.JSON对象转化为JSON字符串 1.使用JSON.stringify()方法进行转换 该方法不支持较老版本的IE浏览器,比如:i ...
- DataPipeline丨LinkedIn元数据之旅的最新进展—Data Hub
作者:Mars Lan, Seyi Adebajo, Shirshanka Das 译者: DataPiepline yaran 作为全球最大的职场社交平台,LinkedIn的数据团队不断致力于扩展其 ...
- python高级编程之 web静态服务器
返回固定数据 import socket def request_handler(new_client_socket): """ 响应客户端请求的核心函数 "& ...