leetcode
leetcode 2151 ~ 2200
回环句

回环句

难度:

标签:

题目描述

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)

代码细节讲解

🦆
你的算法是否考虑了只有一个单词的句子,并且这种情况下算法的处理逻辑是什么?
是的,算法考虑了只有一个单词的情况。对于只有一个单词的句子,因为没有其他单词与之进行比较,所以只需要检查这个单词的最后一个字符是否与其自身的第一个字符相等。根据代码,即使是只有一个单词,也会执行 words[-1][-1] != words[0][0] 的比较,由于它们是同一个单词的首尾字符,通常会相等,因此函数会返回 True。
🦆
在检查最后一个单词的最后一个字符和第一个单词的第一个字符是否相等时,如果句子为空或非常短,会如何处理?
如果句子为空,即没有单词,那么在尝试访问 words[-1] 或 words[0] 时,代码将引发 IndexError,因为 words 列表将是空的。对于非常短的句子,如只有一个字符的单词,该方法仍然有效,因为这种情况下首尾字符相同。然而,对于空句子的情况,代码需要添加特定的错误处理逻辑,以避免运行时错误。
🦆
对于包含大量单词且每个单词只有一个字符的句子,这个算法的效率如何?是否存在更优化的方法?
对于包含大量单词且每个单词只有一个字符的句子,该算法的时间复杂度为 O(n),其中 n 是单词的数量。每个单词仅需与相邻的单词进行一次比较。虽然这种情况下算法已经相当高效,但对于极大规模的数据,常规的字符比较可能会变得耗时。一种可能的优化是在处理前预先验证输入的合理性(如检查输入长度),或者使用并行处理技术分担字符比较的工作。不过,对于大多数实用场景,现有的线性时间复杂度算法已经足够高效。
🦆
在代码实现中,如果连续的两个单词之间存在多个空格,split() 函数的默认行为会产生什么效果?
Python 的 str.split() 函数在没有指定分隔符的情况下,默认会使用空白字符(包括空格、换行符、制表符等)作为分隔符,并且会忽略字符串开头和结尾的空白字符。如果两个单词之间有多个空格,split() 会自动处理这些空格,只将单词作为分割后的元素,不会在结果列表中包含空字符串。因此,多个空格不会影响单词的分割,代码可以正确地将句子分割成单词列表。

相关问题