NeetCode #74LC-2490EasyArrays & Hashing
← Back to All Problems#74 · #2490 · Circular Sentence(循环句子)
📌 Problem Statement & Constraints
A sentence is circular if the last character of each word equals the first character of the next word, including the wrap-around from the last word back to the first. Given a string
sentence, return whether it is circular. Constraints: 1 <= sentence.length <= 500; words are separated by single spaces.💡 Core Algorithmic Approaches
- Split the sentence into words.
- For every adjacent pair, check that the last character of the first word equals the first character of the second.
- Finally check the wrap-around pair: the last character of the final word must equal the first character of the first word.
- A single-word sentence is circular by definition, since the wrap-around pair is the word with itself.
💻 Benchmark Python3 Implementation
class Solution:
def isCircularSentence(self, sentence: str) -> bool:
words = sentence.split()
for i in range(len(words)):
a, b = words[i], words[(i + 1) % len(words)]
if a[-1] != b[0]: # includes the wrap-around pair
return False
return True⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n): splitting is linear in the sentence length and the pairwise checks are O(number of words).
💾 Space Complexity
O(n) for the word list.
⚠️ Interview Pitfalls & Follow-ups
- Only checking adjacent pairs without the wrap-around: the last-to-first pair is explicitly required.
- Assuming a single-word sentence is not circular: with the modulo index, the word is compared with itself, which passes.
- Splitting on
' ': with explicit' 'the words are fine here (single spaces guaranteed), butsplit()is safer against edge cases.