2025-01-09 20:29:41 +08:00
< p > 给定两个字符串 < code > sentence1< / code > 和 < code > sentence2< / code > ,每个表示由一些单词组成的一个句子。句子是一系列由 < strong > 单个 < / strong > 空格分隔的 < strong > 单词< / strong > ,且开头和结尾没有多余空格。每个单词都只包含大写和小写英文字母。< / p >
2022-03-27 20:45:09 +08:00
2025-01-09 20:29:41 +08:00
< p > 如果两个句子 < code > s1< / code > 和 < code > s2< / code > ,可以通过往其中一个句子插入一个任意的句子(可以是空句子)而得到另一个句子,那么我们称这两个句子是 < strong > 相似的< / strong > 。< strong > 注意< / strong > ,插入的句子必须与现有单词用空白隔开。 < / p >
2022-03-27 20:45:09 +08:00
2025-01-09 20:29:41 +08:00
< p > 比方说,< / p >
2022-03-27 20:45:09 +08:00
2025-01-09 20:29:41 +08:00
< ul >
< li > < code > s1 = "Hello Jane"< / code > 与 < code > s2 = "Hello my name is Jane"< / code > ,我们可以往 < code > s1< / code > 中 < code > "Hello"< / code > 和 < code > "Jane"< / code > 之间插入 < code > "my name is"< / code > 得到 < code > s2< / code > 。< / li >
< li > < code > s1 = "Frog cool"< / code > 与 < code > s2 = "Frogs are cool"< / code > 不是相似的,因为尽管往 < code > s1< / code > 中插入 < code > "s are"< / code > ,它没有与 < code > "Frog"< / code > 用空格隔开。< / li >
< / ul >
< p > 给你两个句子 < code > sentence1< / code > 和 < code > sentence2< / code > ,如果< em > < / em > < code > sentence1< / code > 和< em > < / em > < code > sentence2< / code > 是 < strong > 相似< / strong > 的,请你返回 < code > true< / code > ,否则返回 < code > false< / code > 。< / p >
< p > < / p >
2022-03-27 20:45:09 +08:00
< p > < strong > 示例 1: < / strong > < / p >
2025-01-09 20:29:41 +08:00
< div class = "example-block" > < b > 输入:< / b > sentence1 = "My name is Haley", sentence2 = "My Haley"< / div >
< div class = "example-block" > < b > 输出:< / b > true< / div >
< div class = "example-block" > < b > 解释:< / b > 可以往 < code > sentence2< / code > 中 "My" 和 "Haley" 之间插入 "name is" ,得到 < code > sentence1< / code > 。< / div >
< div class = "example-block" > < / div >
2022-03-27 20:45:09 +08:00
< p > < strong > 示例 2: < / strong > < / p >
2025-01-09 20:29:41 +08:00
< div class = "example-block" > < b > 输入:< / b > sentence1 = "of", sentence2 = "A lot of words"< / div >
< div class = "example-block" > < b > 输出:< / b > false< / div >
< div class = "example-block" > < strong > 解释:< / strong > 没法往这两个句子中的一个句子只插入一个句子就得到另一个句子。< / div >
< div class = "example-block" > < / div >
2022-03-27 20:45:09 +08:00
< p > < strong > 示例 3: < / strong > < / p >
2025-01-09 20:29:41 +08:00
< div class = "example-block" > < b > 输入:< / b > sentence1 = "Eating right now", sentence2 = "Eating"< / div >
2022-03-27 20:45:09 +08:00
2025-01-09 20:29:41 +08:00
< div class = "example-block" > < b > 输出:< / b > true< / div >
2022-03-27 20:45:09 +08:00
2025-01-09 20:29:41 +08:00
< div class = "example-block" > < b > 解释:< / b > 可以往 < code > sentence2< / code > 的结尾插入 "right now" 得到 < code > sentence1< / code > 。< / div >
2022-03-27 20:45:09 +08:00
2025-01-09 20:29:41 +08:00
< p > < / p >
2022-03-27 20:45:09 +08:00
< p > < strong > 提示:< / strong > < / p >
< ul >
< li > < code > 1 < = sentence1.length, sentence2.length < = 100< / code > < / li >
2025-01-09 20:29:41 +08:00
< li > < code > sentence1< / code > 和 < code > sentence2< / code > 都只包含大小写英文字母和空格。< / li >
< li > < code > sentence1< / code > 和 < code > sentence2< / code > 中的单词都只由单个空格隔开。< / li >
2022-03-27 20:45:09 +08:00
< / ul >