原题链接在这里: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. laravels 使用laravel-wechat 组件

    一. laravels (5.4)使用 laravel-wechat (4.13),出现无法登陆的情况,显示没有code 错误,解决办法 前提:已经在laravels.php 的 cleaners 中 ...

  2. Linux指定运行级别,帮助指令(man,help)

    运行级别说明: 0:关机 1:单用户[找回丢失密码] 2:多用户状态[无网络服务] 3:多用户状态[有网络服务] 4:保留级别 5:图形界面 6:系统重启 一.指定运行级别 1.修改默认运行级别 vi ...

  3. Promise实现子组件的多表单校验并反馈结果给父组件

    全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/11529207.html,多谢,=.=~ 本文中多表单验证主要用到Promise.all()实现多 ...

  4. RabbitMQ学习之Routing(4)

    上一节,是广播日志message到很多的receivers. 这节,我们讲订阅其中的一个子集.例如,我们想可以把危机的error message导到log file.而仍然可以打印所有的log mes ...

  5. .NET中的泛型概述

    什么是泛型? 泛型是具有占位符(类型参数)的类.结构.接口和方法,这些占位符是类.结构.接口和方法所存储或使用的一个或多个类型的占位符.泛型集合类可以将类型形参用作其存储的对象类型的占位符:类型形参呈 ...

  6. [转]js获取iframe通过url传递的参数

    1.前言 获取iframe的url路径:window.parent.document.getElementById("your-iframe-id").contentWindow. ...

  7. 【JVM学习笔记一】Java内存区域

    1. 运行时数据区域 1) 程序计数器 | 线程私有,存储线程运行时所执行字节码的行号,实现分支.循环.跳转.异常处理.线程恢复等基础功能 | Java方法,记录正在执行的虚拟机字节码指令的行号:Na ...

  8. 组件切换方式(Vue.js)

    这里,我用一个注册登录两组件的切换实例来演示: 切换方式一 <!DOCTYPE html> <html lang="zh-CN"> <head> ...

  9. Spark(火花)快速、通用的大数据处理引擎框架

    一.什么是Spark(火花)? 是一种快速.通用处理大数据分析的框架引擎. 二.Spark的四大特性 1.快速:Spark内存上采用DAG(有向无环图)执行引擎非循环数据流和内存计算支持. 内存上比M ...

  10. 关于连接不上SVN的部分解决方案——No repository found in svn localhost

    今天如往常一样做事,期间发现一个问题,于是就打算将文件与 svn 上的文件进行对比,可谁成想 Eclipse 突然弹框报错,然后我到SVN资源库中直接刷新打开 svn 的地址,又弹框报错:文件夹不存在 ...