LeetCode 题解工作台

回环句

句子 是由单个空格分隔的一组单词,且不含前导或尾随空格。 例如, "Hello World" 、 "HELLO" 、 "hello world hello world" 都是符合要求的句子。 单词 仅 由大写和小写英文字母组成。且大写和小写字母会视作不同字符。 如果句子满足下述全部条件,则认为它是一…

category

1

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · String-driven solution strategy

bolt

答案摘要

我们将字符串按照空格分割成单词,然后判断每个单词的最后一个字符和下一个单词的第一个字符是否相等,如果不相等则返回 `false`,否则遍历完所有单词后返回 `true`。 时间复杂度 ,空间复杂度 。其中 是字符串的长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 String-driven solution strategy 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

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

  • 例如,"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 中的单词由单个空格进行分隔
  • 不含任何前导或尾随空格
lightbulb

解题思路

方法一:模拟

我们将字符串按照空格分割成单词,然后判断每个单词的最后一个字符和下一个单词的第一个字符是否相等,如果不相等则返回 false,否则遍历完所有单词后返回 true

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 是字符串的长度。

1
2
3
4
5
6
class Solution:
    def isCircularSentence(self, sentence: str) -> bool:
        ss = sentence.split()
        n = len(ss)
        return all(s[-1] == ss[(i + 1) % n][0] for i, s in enumerate(ss))
speed

复杂度分析

指标
时间O(n)
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Mentions checking each word's last and first characters for a circular pattern.

  • question_mark

    Asks about handling single-word sentences and case sensitivity.

  • question_mark

    Looks for an efficient linear scan instead of multiple splits or extra storage.

warning

常见陷阱

外企场景
  • error

    Ignoring case sensitivity between characters when comparing.

  • error

    Failing to check the connection from the last word back to the first word.

  • error

    Assuming extra spaces or empty words without verifying input constraints.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow punctuation and special characters and check circularity ignoring non-letter symbols.

  • arrow_right_alt

    Determine circularity in reversed sentences or backwards word order.

  • arrow_right_alt

    Check for partial circular patterns where only a subset of words form a loop.

help

常见问题

外企场景

回环句题解:String-driven solution … | LeetCode #2490 简单