LeetCode Problem Workspace
Circular Sentence
Determine if a sentence is circular by verifying each word's last character matches the next word's first character efficiently.
1
Topics
7
Code langs
3
Related
Practice Focus
Easy · String-driven solution strategy
Answer-first summary
Determine if a sentence is circular by verifying each word's last character matches the next word's first character efficiently.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
This problem requires checking if a sentence forms a loop where each word's last letter equals the next word's first letter. Single-word sentences are circular by definition. Using a string-driven strategy, iterate through the sentence and compare characters around spaces for a direct and efficient solution.
Problem Statement
You are given a sentence consisting of words separated by single spaces with no leading or trailing spaces. Each word contains only uppercase or lowercase English letters, which are case-sensitive.
A sentence is considered circular if every word's last character matches the first character of the next word, and the last word's last character matches the first word's first character. Return true if the sentence is circular, otherwise return false.
Examples
Example 1
Input: sentence = "leetcode exercises sound delightful"
Output: true
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
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
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.
Solution Approach
Split Sentence and Compare Characters
Divide the sentence into words and iterate through each word. Compare the last character of the current word with the first character of the next word. Ensure the last word connects back to the first word to complete the circular check.
Single-Pass Character Check
Instead of splitting, iterate through the sentence and check characters before and after each space. Compare adjacent word boundaries in one pass to maintain O(n) time and O(1) space complexity.
Handle Edge Cases
Consider single-word sentences as circular by default. Also, account for case sensitivity since 'A' and 'a' are different. Ensure no extra spaces or empty words interfere with comparisons.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(n) |
| Space | O(1) |
The solution iterates through each character in the sentence once, giving O(n) time complexity. Only a few pointers or indices are used, resulting in O(1) extra space.
What Interviewers Usually Probe
- Mentions checking each word's last and first characters for a circular pattern.
- Asks about handling single-word sentences and case sensitivity.
- Looks for an efficient linear scan instead of multiple splits or extra storage.
Common Pitfalls or Variants
Common pitfalls
- Ignoring case sensitivity between characters when comparing.
- Failing to check the connection from the last word back to the first word.
- Assuming extra spaces or empty words without verifying input constraints.
Follow-up variants
- Allow punctuation and special characters and check circularity ignoring non-letter symbols.
- Determine circularity in reversed sentences or backwards word order.
- Check for partial circular patterns where only a subset of words form a loop.
FAQ
What is the circular sentence pattern in this problem?
The circular sentence pattern requires each word's last character to match the next word's first character, looping back to the first word.
Does a single-word sentence count as circular?
Yes, any single-word sentence is considered circular since its first and last character are the same word boundaries.
Are uppercase and lowercase letters treated the same?
No, the solution is case-sensitive. 'A' and 'a' are considered different characters for circular checks.
What is the optimal time complexity for checking a circular sentence?
The optimal approach scans the sentence once, giving O(n) time complexity with O(1) additional space.
Can this method handle sentences with multiple spaces?
No, input constraints guarantee single spaces between words, so the method assumes no extra spaces.
Solution
Solution 1: Simulation
We split the string into words by spaces, then check whether the last character of each word is equal to the first character of the next word. If they are not equal, return `false`. Otherwise, return `true` after traversing all the words.
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))Solution 2: Simulation (Space Optimization)
We can first check whether the first and last characters of the string are equal. If they are not equal, return `false`. Otherwise, traverse the string. If the current character is a space, check whether the previous character and the next character are equal. If they are not equal, return `false`. Otherwise, return `true` after traversing all the characters.
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))Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward