跳至主要內容

2490. 回环句


2490. 回环句open in new window

🟢   🔖  字符串  🔗 力扣open in new window LeetCodeopen in new window

题目

A sentence is a list of words that are separated by asingle space with no leading or trailing spaces.

  • For example, "Hello World", "HELLO", "hello world hello world" are all sentences.

Words consist of only uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.

A sentence is circular if:

  • The last character of a word is equal to the first character of the next word.
  • The last character of the last word is equal to the first character of the first word.

For example, "leetcode exercises sound delightful", "eetcode", "leetcode eats soul" are all circular sentences. However, "Leetcode is cool", "happy Leetcode", "Leetcode" and "I like Leetcode" are not circular sentences.

Given a string sentence, return true if it is circular. Otherwise, return false.

Example 1:

Input: sentence = "leetcode exercises sound delightful"

Output: true

Explanation: The words in sentence are ["leetcode", "exercises", "sound", "delightful"].

  • leetcod e 's last character is equal to e xercises's first character.
  • exercise s 's last character is equal to s ound's first character.
  • soun d 's last character is equal to d elightful's first character.
  • delightfu l 's last character is equal to l eetcode's first character.

The sentence is circular.

Example 2:

Input: sentence = "eetcode"

Output: true

Explanation: The words in sentence are ["eetcode"].

  • eetcod e 's last character is equal to e etcode's first character.

The sentence is circular.

Example 3:

Input: sentence = "Leetcode is cool"

Output: false

Explanation: The words in sentence are ["Leetcode", "is", "cool"].

  • Leetcod e 's last character is not equal to i s's first character.

The sentence is not circular.

Constraints:

  • 1 <= sentence.length <= 500
  • sentence consist of only lowercase and uppercase English letters and spaces.
  • The words in sentence are separated by a single space.
  • There are no leading or trailing spaces.

题目大意

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

  • 例如,"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"] 。

  • leetcod** e** 的最后一个字符和 e xercises 的第一个字符相等。
  • exercise s 的最后一个字符和 s ound 的第一个字符相等。
  • s ound 的最后一个字符和 delightfu l 的第一个字符相等。
  • delightfu l 的最后一个字符和 l eetcode 的第一个字符相等。

这个句子是回环句。

示例 2:

输入: sentence = "eetcode"

输出: true

解释: 句子中的单词是 ["eetcode"] 。

  • eetcod e 的最后一个字符和 e etcod e 的第一个字符相等。

这个句子是回环句。

示例 3:

输入: sentence = "Leetcode is cool"

输出: false

解释: 句子中的单词是 ["Leetcode", "is", "cool"] 。

  • Leetcod e 的最后一个字符和 i s 的第一个字符 相等。

这个句子 是回环句。

提示:

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

解题思路

  1. 先将输入的字符串 sentence 按空格分割成一个单词数组 arr
  2. 遍历数组,依次对比当前单词的最后一个字符 lastChar 与下一个单词的第一个字符 nextFirstChar 是否一样;
  3. 注意边界情况,最后一个单词的最后一个字符,要和第一个单词的第一个字符相等;
  4. 一旦发现不相等的情况,立刻返回 false
  5. 遍历完还没有发现不相等的情况,则返回 true

复杂度分析

  • 时间复杂度O(n),其中 n 是句子中的单词数量,需要遍历所有的单词。
  • 空间复杂度O(n),需要额外的数组存储所有单词。

代码

/**
 * @param {string} sentence
 * @return {boolean}
 */
var isCircularSentence = function (sentence) {
	const arr = sentence.split(' '),
		n = arr.length;
	for (let i = 0; i < n; i++) {
		const lastChar = arr[i][arr[i].length - 1];
		const nextFirstChar = arr[(i + 1) % n][0];
		if (lastChar !== nextFirstChar) return false;
	}
	return true;
};

相关题目

题号标题题解标签难度
1652拆炸弹open in new window数组 滑动窗口