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.
This method involves splitting the sentence into words and verifying the circular property by manually comparing the last character of one word with the first character of the next word, including checking the condition from the last word to the first word.
This solution iterates through the given sentence, considering spaces as word boundaries. It checks every word's last character against the next one's first character and finally checks the sentence's first and last character.
Time complexity: O(n), where n is the length of the sentence.
Space complexity: O(1), as no additional data structures are used.
This approach uses two pointers to traverse through the sentence. One pointer identifies word endings, the other starts at the first character of new words. This methodology avoids splitting the sentence into words explicitly.
This C code processes the string with a single for-loop, using indexes to track word boundaries and assessing the circular criteria directly.
Time complexity: O(n), due to the traversal of the sentence.
Space complexity: O(1), as it involves no auxiliary storage.
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.
The time complexity is O(n), and the space complexity is O(n). Where n is the length of the string.
Python
Java
C++
Go
TypeScript
Rust
JavaScript
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.
The time complexity is O(n), where n is the length of the string. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
Rust
JavaScript
| Approach | Complexity |
|---|---|
| Using a Loop with String Manipulation | Time complexity: O(n), where n is the length of the sentence. |
| Using a Single Pass with Two Pointers | Time complexity: O(n), due to the traversal of the sentence. |
| Simulation | — |
| Simulation (Space Optimization) | — |
| 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 |
Circular Sentence - Leetcode 2490 - Python • NeetCodeIO • 5,423 views views
Watch 9 more video solutions →Practice Circular Sentence with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor