题目地址:https://leetcode-cn.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:

  1. The length of words1 and words2 will not exceed 1000.
  2. The length of pairs will not exceed 2000.
  3. The length of each pairs[i] will be 2.
  4. The length of each words[i] and pairs[i][j] will be in the range [1, 20].

题目大意

给定两个句子 words1, words2 (每个用字符串数组表示),和一个相似单词对的列表 pairs ,判断是否两个句子是相似的。

解题方法

只修改区间起终点

这个题有一个坑的地方没说清楚:一个词可能会和多个词相似。

因此使用map<string, set>的方式,保存{每个词 : 与之相似的词}所有映射关系。

注意相似是双向的,因此对于一个Pair,需要正反插入两次。

判断是否相似的时候,需要对两个列表的对应元素进行遍历,判断是否相等或者在其相似set中出现。

C++代码如下:

class Solution {
public:
bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<vector<string>>& pairs) {
if (words1.size() != words2.size()) return false;
const int N = words1.size();
unordered_map<string, unordered_set<string>> similar;
for (auto& pair : pairs) {
similar[pair[0]].insert(pair[1]);
similar[pair[1]].insert(pair[0]);
}
for (int i = 0; i < N; ++i) {
if (words1[i] != words2[i] && !similar[words1[i]].count(words2[i])) {
return false;
}
}
return true;
}
};

日期

2019 年 9 月 18 日 —— 今日又是九一八

【LeetCode】734. Sentence Similarity 解题报告(C++)的更多相关文章

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

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

  2. LeetCode 734. Sentence Similarity

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

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

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

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. 系统发育树邻接法(NJ)和非加权组平均法(UPGMA)之比较

    目录 1.原理的区别 2.实操比较 UPGMA NJ法 保存树文件 更深理解 1.原理的区别 主要区别在于,非加权组平均法(UPGMA)是基于平均链接方法的聚集层次聚类方法,而邻接法(NJ)是基于最小 ...

  2. 【5】肿瘤DNA甲基化数据分析原理及流程

    目录 导论 DNA甲基化基本概论 检测DNA甲基化的方法 DNA甲基化数据分析流程及方法 DNA甲基化在肿瘤研究中的应用 导论 表观遗传:非DNA决定的基因表达,或表型改变中可遗传因素的研究 DNA水 ...

  3. 52-Linked List Cycle

    Linked List Cycle My Submissions QuestionEditorial Solution Total Accepted: 102785 Total Submissions ...

  4. 商业爬虫学习笔记day7-------解析方法之bs4

    一.Beautiful Soup 1.简介 Beautiful Soup 是python的一个库,最主要的功能是从网页抓取数据.其特点如下(这三个特点正是bs强大的原因,来自官方手册) a. Beau ...

  5. Docker学习(四)——Docker容器连接

    Docker容器连接     容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过-P或-p参数来指定端口映射. 下面我们来实现通过端口连接到一个docker容器. 1.网络端口映射    ...

  6. window ntp服务

    一.确定服务端和客户端处于同一网段,能相互 Ping 通. 二.服务器端(Server)配置 1.选择一台服务器(Windows 系统)作为时间同步服务器: 2.Win + R (运行 cmd 命令行 ...

  7. springboot-使用AOP日志拦截实现

    一 前言 借助spring的AOP功能,我们可以将AOP应用至全局异常处理,全局请求拦截等,本篇文章的核心功能就是使用AOP实现日志记录,比如哪些用户进行了哪些操作,对于一个成功的项目这是必须记录的, ...

  8. springboot热部署与监控

    一.热部署 添加依赖+Ctrl+F9 <dependency> <groupId>org.springframework.boot</groupId> <ar ...

  9. HashMap 和 HashSet

    对于HashSet而言,系统采用Hash算法决定集合元素的存储位置,这样可以保证快速存取集合元素: 对于HashMap,系统将value当成key的附属,系统根据Hash算法来决定key的存储位置,这 ...

  10. 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy

    Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...