Watch 10 video solutions for Circular Sentence, a easy level problem involving String. This walkthrough by NeetCodeIO has 5,423 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
"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:
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 <= 500sentence consist of only lowercase and uppercase English letters and spaces.sentence are separated by a single space.Problem Overview: A sentence is called circular if the last character of each word matches the first character of the next word, and the last word connects back to the first. Words are separated by spaces. You need to verify this circular property across the entire sentence.
Approach 1: Using a Loop with String Manipulation (Time: O(n), Space: O(1))
The direct way is to split the sentence into words and compare adjacent pairs. First check that the last character of the final word equals the first character of the first word. Then iterate through the list of words and verify that words[i][-1] == words[i+1][0] for every pair. If any comparison fails, the sentence is not circular.
This approach relies entirely on basic string manipulation. Splitting the sentence produces an array of words, and a simple loop checks the circular condition. The runtime is O(n) because each character in the sentence is processed at most once during splitting and comparison. Extra space is technically O(k) for the words array, but it’s usually treated as O(1) relative to the input size since no additional data structures are required.
This version is easy to reason about and works well during interviews when readability matters. The logic mirrors the problem statement directly, which reduces the chance of off‑by‑one mistakes.
Approach 2: Single Pass with Two Pointers (Time: O(n), Space: O(1))
You can avoid splitting the string by scanning the sentence once and checking transitions between words. While iterating through characters, every time you encounter a space, compare the character before it with the character after it. These represent the last letter of the current word and the first letter of the next word.
Two indices (or a simple index scan) act as two pointers to verify boundaries between words. If sentence[i] == ' ', check whether sentence[i-1] == sentence[i+1]. After the loop finishes, perform one more comparison between the last character of the sentence and the first character to complete the circular condition.
This method keeps everything in the original string and avoids creating a words array. The algorithm performs a single traversal of the string, giving O(n) time complexity and O(1) space. It’s slightly more efficient in practice and demonstrates strong control over string indexing.
Recommended for interviews: Both solutions run in O(n) time, but interviewers usually expect the single-pass scan because it avoids unnecessary allocations and shows comfort with string indexing. The split-and-loop method is still valuable—it shows clear reasoning and is often the fastest way to reach a correct answer before optimizing.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Loop with String Manipulation | O(n) | O(1) | Best for clarity and quick implementation using split words |
| Single Pass with Two Pointers | O(n) | O(1) | Preferred in interviews when minimizing extra allocations |