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.In #2490 Circular Sentence, the goal is to verify whether a sentence forms a circular chain of words. A sentence is considered circular if the last character of each word matches the first character of the next word, and the last word also connects back to the first word.
A straightforward approach is to split the sentence into words and compare adjacent pairs. For each pair of words, check whether the last character of the current word equals the first character of the next word. After validating all pairs, ensure the last word’s final character matches the first character of the first word to complete the circle.
An alternative is to scan the string directly without fully splitting it, checking characters around spaces and verifying the first and last characters of the entire sentence. Both methods run in linear time relative to the sentence length and require minimal extra space.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Split sentence and compare adjacent words | O(n) | O(n) |
| Single pass string scan | O(n) | O(1) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
Check the character before the empty space and the character after the empty space.
Check the first character and the last character of the sentence.
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.
Time complexity: O(n), where n is the length of the sentence.
Space complexity: O(1), as no additional data structures are used.
1def is_circular_sentence(sentence):
2 words = sentence.split()
3 if not words:
4 return False
5
6 first_char =
The Python solution splits the sentence into a list of words and checks the last character of each word against the first character of the next word. It further checks the consistency between the first and last word.
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.
Time complexity: O(n), due to the traversal of the sentence.
Space complexity: O(1), as it involves no auxiliary storage.
1
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Problems like Circular Sentence are common in coding interviews to test basic string manipulation and edge-case handling. While the exact problem may not always appear, similar string validation problems are frequently asked in technical interviews.
The optimal approach is to scan the sentence and compare boundary characters of words. Check that the last character before every space matches the first character after it, and verify that the last character of the sentence equals the first character. This can be done in a single linear pass.
This problem mainly relies on string processing, so no advanced data structure is required. You can either split the string into an array of words or traverse the string directly using indices for an O(1) space solution.
Yes, you can solve it by scanning the string once and checking characters around spaces. Whenever a space is found, compare the character before it with the character after it, and finally verify the circular condition between the first and last characters.
This Python code traverses the sentence with a straightforward loop to monitor space-separated boundaries and verify character connectivity in a circular fashion.