[Leetcode] 734. Sentence Similarity 解题报告

【LeetCode Python实现】734. 句子相似性(简单) 文章目录题目描述示例 1:示例 2:示例 3:提示:参考代码 题目描述 我们可以将一个句子表示为一个单词数组,例如,句子 “I am happy with leetcode” 可以表示为 arr = [“I”,“am”,happy",“with”,“leetcode”] 给定两个句子 sentence1 和 sentence2 分别表示为一个字符串数组,并给定一个字符串对 similarPairs ,其中 similarPairs[i] = [xi, yi] 表示两个单词 xi and yi 是相似的。 阅读详情

题目:

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].

    思路:

    我们可以根据pairs中的内容,建立起字符串到字符串之间的映射,不过需要注意到一个字符串可能会有多个字符串和它类似。为了减少存储空间,我在建立映射的过程中,约定将大的字符串映射到小的字符串中。

    当有了字典之后,我们就依次遍历words1和words2中的对应字符串,看看他们的相似关系是否可以在字典中找到。如果找不到就提前返回false,否则最后返回true。

    代码:

    class Solution {
    public:
        bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
            if (words1.size() != words2.size()) {
                return false;
            }
            unordered_map<string, set<string>> hash;     // map from big word to small word
            for (auto it = pairs.begin(); it != pairs.end(); ++it) {
                if (it->first < it->second) {
                    hash[it->second].insert(it->first);
                }
                else {
                    hash[it->first].insert(it->second);
                }
            }
            for (int i = 0; i < words1.size(); ++i) {
                if (words1[i] < words2[i]) {
                    if (hash.count(words2[i]) == 0 || hash[words2[i]].find(words1[i]) == hash[words2[i]].end()) {
                        return false;
                    }
                }
                else if (words1[i] > words2[i]) {
                    if (hash.count(words1[i]) == 0 || hash[words1[i]].find(words2[i]) == hash[words1[i]].end()) {
                        return false;
                    }
                }
            }
            return true;
        }
    };
51单片机——LCD12864驱动函数深度解析与实战优化 本文深入解析了51单片机驱动LCD12864液晶屏的核心驱动函数,从底层时序、关键延时到地址计算进行逐行剖析,并针对常见显示问题提供系统性的调试技巧与实战优化方案。重点探讨了如何将基础代码优化为稳定高效的驱动,涵盖延时精准化、批量写入及显示缓冲区等高级技巧,旨在帮助开发者从“点亮”屏幕进阶到“精通”驱动,确保显示稳定可靠。 阅读详情

相关推荐

PHP特性知识点总结

(1)类型转换之前提到过的是字符串转为整型。其实php手册里面提到了各种类型的转换。先把他放在这里。我们按照php手册上的顺序来介绍,只介绍几个比较常用的。①转化为bool②转化为整型int。

m0_73605862的博客 1295

leetcode | go | 第734题 | 句子相似性

句子相似性

iCling的博客 505

深信服技术认证“SCCA-C”划重点:信服云托管云

该认证包括云数据中心基础、云计算基础、云计算关键技术、云计算交付和运维体系、云计算技术商业应用、信服云托管云、深信服超融合HCI、深信服桌面云aDesk、深信服分布式存储EDS、深信服应用交付AD。学习并获得SCCA-C认证后,可以具备深信服产品功能原理与实践,能够根据业务需求,协助完成云产品方案落地与实施;能够根据业务需求,完成深信服云产品的部署和管理的基本工作。为帮助大家更加系统化地学习云计算知识,高效通过云计算工程师认证,深信服特推出“SCCA-C认证备考秘笈”,共十期内容。

sangfor_edu的博客 613

leetcode 算法题734 (简单192) 句子的相似性

leetcode 算法题734 (简单192) 句子的相似性 题目介绍 给定两个句子 words1, words2 (每个用字符串数组表示), 和一个相似单词对的列表 pairs ,判断是否两个句子是相似的。 例如,当相似单词对是 pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]]的时候,...

FYuu95100的博客 1302

从零到一:使用阿里云百炼搭建企业级 AI Agent 完全实战手册(2026超详细版)

本文是一份企业级AI Agent构建手册,主要面向企业技术负责人、AI产品经理和后端开发工程师。手册详细介绍了如何在阿里云百炼平台上搭建企业智能体应用的全流程,包括环境准备、知识库构建、智能体创建、工具集成、工作流编排等核心环节。重点内容包括: 准备工作:阿里云账号注册、百炼服务开通、免费额度领取、API Key管理 知识库构建:文档格式要求、内容优化原则、RAG系统搭建 智能体开发:Agent创建、工具集成、复杂业务工作流设计 生产部署:多渠道接入、监控运维、安全治理 成本控制:Token预算管理、选型决

u010323587的专栏 1079

表格数据学习的图神经网络综述

今天学习一篇发表在中科院一区期刊《ACM Computing Surveys》的综述文章《Graph Neural Networks for Tabular Data Learning: A Survey with Taxonomy & Directions》,该刊主要刊载计算机理论方法领域的系统性综述与教程论文,注重跨学科技术融合研究,最新的影响因子为28.0。本文的目的在于通过学习这篇文章,了解关于表格数据学习点图神经网络一些基础知识。

qq_41009600的博客 799

【LeetCode - 734】句子相似性

【简单】哈希表

学哥斌的博客 784

【笔记篇】Davinci Configurator OS模块(下)

AUTOSAR OS的配置项和Vevtor MICROSAR OS的特性

weixin_47571898的博客 1153

Leetcode—734. 句子相似性【简单】Plus

运行结果 之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

Atticus的博客 257

【音视频】PJSIP库——示例简介、C++类说明

aectest音频回声消除测试工具,用于演示音频处理模块中的回声消除(AEC)功能,通常用于 VoIP 场景中优化语音质量。auddemo音频播放 / 录制演示,展示如何使用 PJProject 的音频接口进行基本的音频数据采集、播放和处理,例如波形显示或格式转换。stereotest立体声处理测试,可能用于演示多声道音频的处理方式,如混音、声道分离等。tonegen音频信号生成工具,用于生成测试用的音频信号(如正弦波、音调等),常用于音频设备测试或调试。recfile。

郭老二 434

汽车安全新防线|基于AI的入侵检测系统开发(全网独家复现),CNN-BiLSTM融合改进篇 强化CAN总线数据感知、助力多类型入侵检测、实时响应预警、车载场景有效落地

基于AI的入侵检测系统是汽车网络安全领域的重要技术突破,能够有效应对日益复杂的网络攻击威胁。随着AI技术的不断发展和普及,AI入侵检测系统将在汽车安全领域发挥越来越重要的作用。对于汽车制造商和供应商来说,实施AI入侵检测系统不仅是保障车辆安全的需要,也是提升市场竞争力的重要手段。未来,AI入侵检测系统将成为智能汽车的标准配置,为用户提供更加安全、可靠的驾驶体验。

getusushu的博客 85

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

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法只修改区间起终点日期 题目地址:https://leetcode-cn.com/problems/sentence-similarity/ 题目描述 Given two sentences words1, words2 (each repr...

负雪明烛 1084

二、FM1288调试方案-工具软件使用

分析音频数据的软件需要满足以下几点:1、能打开原始pcm音频文件;2、基本的音频播放软件的操作;3、能查看分析音频数据的频率成分;4、能查看分析音频音量幅度;5、能完成基本音频处理:降噪、音量增幅限幅、滤波和均衡;

优快云mianfeixiazai001的博客 1293

【Leetcode】734. Sentence Similarity

题目地址: https://leetcode.com/problems/sentence-similarity/ 给定一个列表,列表中每个位置都是一个长为222的字符串数组,规定数组内的字符串是”相似的“。再给定两个字符串数组,问这两个数组是否对应位置的字符串都相似。注意,规定”相似关系“不具有传递性,只具有对称性。如果两个数组长度不等则直接返回false;并且如果对应位置上的字符串相等,也视为相...

数学、算法爱好者的博客 269

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode | 734. Sentence Similarity

Given two sentences words1,words2 (each represented as an array of strings), and a listof similar word pairs pairs,determine if two sentences are similar. For example, "great acting skills" and"fin

u012737193的博客 402

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

[leetcode] 734. Sentence Similarity @ python

原题 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 ...

闲庭信步 589

LeetCode #734 - 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"...

LawFile的博客 263

[LeetCode] 734. 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 "f...

weixin_30883271的博客 409

leetcode734- Sentence Similarity- easy

Given two sentenceswords1, words2(each represented as an array of strings), and a list of similar word pairspairs, determine if two sentences are similar. For example, "great acting skills" and "f...

weixin_30287169的博客 173

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

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, words1 = ["great", "acting",...

weixin_30770495的博客 513

LeetCode 734. 句子相似性(哈希)

文章目录1. 题目2. 解题 1. 题目 给定两个句子 words1, words2 (每个用字符串数组表示),和一个相似单词对的列表 pairs ,判断是否两个句子是相似的。 例如,当相似单词对是 pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]]的时候,"great acting skills" 和 "fine drama talent" 是相似的。 注意相似关系是不具有传递性的。 例如,如果 “great” 和 “f

Michael是个半路程序员 2287

734. 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

New coder, Step further 525

LWC 60:734. Sentence Similarity

LWC 60:734. Sentence Similarity传送门:734. Sentence SimilarityProblem: Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if

Demon-初来驾到 827

LeetCode-Algorithms-[Easy]734. 句子相似性

734. 句子相似性 public boolean areSentencesSimilar(String[] words1, String[] words2, List<List<String>> pairs) { int m = words1.length; int n = words2.length; if (m != n) return f...

AlphaCat的博客 637

为了节约存储容量,需要压缩字符串,请写出一段程序,可以将字符串按照以下规则压缩并输出

为了节约存储容量,需要压缩字符串,请写出一段程序,可以将字符串按照以下规则压缩并输出。 压缩规则为:相同字符串连续,则压缩为“字符串个数+字符”,如“aaabb”压缩为“3a2b” public static String changeStr(String str) { StringBuffer sb = new StringBuffer(); int count = str...

su1177的博客 1278
上一篇: [Leetcode] 733. Flood Fill 解题报告
下一篇: [Leetcode] 735. Asteroid Collision 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值