「每日LeetCode」2022年12月20日

本文最后更新于:2023年3月19日 晚上

2490.回环句

2490.回环句

Category Difficulty Likes Dislikes
algorithms Easy (71.16%) 6 -

Tags
Companies
句子 是由单个空格分隔的一组单词,且不含前导或尾随空格。

  • 例如,”Hello World”、”HELLO”、”hello world hello world” 都是符合要求的句子。

单词 由大写和小写英文字母组成。且大写和小写字母会视作不同字符。
如果句子满足下述全部条件,则认为它是一个 回环句

  • 单词的最后一个字符和下一个单词的第一个字符相等。
  • 最后一个单词的最后一个字符和第一个单词的第一个字符相等。

例如,”leetcode exercises sound delightful”、”eetcode”、”leetcode eats soul” 都是回环句。然而,”Leetcode is cool”、”happy Leetcode”、”Leetcode” 和 “I like Leetcode” 都 是回环句。
给你一个字符串 sentence ,请你判断它是不是一个回环句。如果是,返回 true_ _;否则,返回 false 。

示例 1:
输入:sentence = “leetcode exercises sound delightful” 输出:true 解释:句子中的单词是 [“leetcode”, “exercises”, “sound”, “delightful”] 。 - leetcode 的最后一个字符和 exercises 的第一个字符相等。 - exercises 的最后一个字符和 sound 的第一个字符相等。 - sound 的最后一个字符和 delightful 的第一个字符相等。 - delightful 的最后一个字符和 leetcode 的第一个字符相等。 这个句子是回环句。
示例 2:
输入:sentence = “eetcode” 输出:true 解释:句子中的单词是 [“eetcode”] 。 - eetcode 的最后一个字符和 eetcode 的第一个字符相等。 这个句子是回环句。
示例 3:
输入:sentence = “Leetcode is cool” 输出:false 解释:句子中的单词是 [“Leetcode”, “is”, “cool”] 。 - Leetcode  的最后一个字符和 is 的第一个字符 不 相等。 这个句子 不 是回环句。

提示:

  • 1 <= sentence.length <= 500
  • sentence 仅由大小写英文字母和空格组成
  • sentence 中的单词由单个空格进行分隔
  • 不含任何前导或尾随空格

Discussion | Solution

思路

首先将句子按照空格分割成单词数组,然后使用 JavaScript 的 Array.prototype.every() 方法遍历数组中的每个单词,对于每个单词,获取它后面的单词(通过取余运算得到下一个单词的索引),并判断当前单词的最后一个字母是否等于下一个单词的第一个字母。如果对于数组中的所有单词都成立,则返回 true,否则返回 false。

解答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* @lc app=leetcode.cn id=2490 lang=javascript
*
* [2490] 回环句
*/

// @lc code=start
/**
* @param {string} sentence
* @return {boolean}
*/
var isCircularSentence = function (sentence) {
sentence = sentence.split(" ");
return sentence.every((word, index) => {
const nextWord = sentence[(index + 1) % sentence.length];
const last = word.at(-1),
first = nextWord.at(0);
return last == first;
});
};
// @lc code=end