回环句
难度:
标签:
题目描述
A sentence is a list of words that are separated by a single 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"]. - leetcode's last character is equal to exercises's first character. - exercises's last character is equal to sound's first character. - sound's last character is equal to delightful's first character. - delightful's last character is equal to leetcode's first character. The sentence is circular.
Example 2:
Input: sentence = "eetcode" Output: true Explanation: The words in sentence are ["eetcode"]. - eetcode's last character is equal to eetcode'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"]. - Leetcode's last character is not equal to is'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.
代码结果
运行时间: 26 ms, 内存: 16.0 MB
/*
* 思路:
* 1. 使用 Java Stream 将句子分割成单词数组。
* 2. 利用 IntStream 检查每个单词的最后一个字符是否与下一个单词的第一个字符相同。
* 3. 检查最后一个单词的最后一个字符是否与第一个单词的第一个字符相同。
* 4. 如果所有条件满足,则返回 true,否则返回 false。
*/
import java.util.stream.*;
public class CircularSentenceStream {
public static boolean isCircularSentence(String sentence) {
String[] words = sentence.split(" ");
return IntStream.range(0, words.length)
.allMatch(i -> words[i].charAt(words[i].length() - 1) == words[(i + 1) % words.length].charAt(0));
}
public static void main(String[] args) {
System.out.println(isCircularSentence("leetcode exercises sound delightful")); // true
System.out.println(isCircularSentence("eetcode")); // true
System.out.println(isCircularSentence("Leetcode is cool")); // false
}
}
解释
方法:
此题解的思路首先是将给定的句子拆分成单词列表。之后,首先检查句子的第一个单词的首字母与最后一个单词的末字母是否相等。如果不相等,则直接返回 false,因为这不满足回环句的定义。接着,代码会遍历单词列表,确保每个单词的最后一个字母与下一个单词的第一个字母相等。如果所有检查都通过,则句子是一个回环句,否则不是。
时间复杂度:
O(n)
空间复杂度:
O(n)
代码细节讲解
🦆
你的算法是否考虑了只有一个单词的句子,并且这种情况下算法的处理逻辑是什么?
▷🦆
在检查最后一个单词的最后一个字符和第一个单词的第一个字符是否相等时,如果句子为空或非常短,会如何处理?
▷🦆
对于包含大量单词且每个单词只有一个字符的句子,这个算法的效率如何?是否存在更优化的方法?
▷🦆
在代码实现中,如果连续的两个单词之间存在多个空格,split() 函数的默认行为会产生什么效果?
▷