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.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.
C++
Java
Python
C#
JavaScript
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.
C++
Java
Python
C#
JavaScript
Time complexity: O(n), due to the traversal of the sentence.
Space complexity: O(1), as it involves no auxiliary storage.
| 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. |
Circular Sentence - Leetcode 2490 - Python • NeetCodeIO • 5,106 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