Difficulty: Easy | Acceptance: 70.20% | Paid: No Topics: String
A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
For example, “Hello World”, “HELLO”, “hello world hello world” are all sentences. Words consist of only uppercase and lowercase English letters.
A sentence can be circular if:
- The last character of a word is equal to the first character of the next word.
- The last character of the last word is equal to the first character of the first word.
Given a string sentence, return true if the sentence is circular, otherwise return false.
- Examples
- Constraints
- Split and Compare
- Single Pass Without Splitting
Examples
Input: sentence = "leetcode exercises sound delightful"
Output: true
Explanation: The last character of "leetcode" is 'e', which is equal to the first character of "exercises". The last character of "exercises" is 's', which is equal to the first character of "sound". The last character of "sound" is 'd', which is equal to the first character of "delightful". The last character of "delightful" is 'l', which is equal to the first character of "leetcode".
Input: sentence = "eetcode"
Output: true
Explanation: The sentence has only one word, so it's circular if the first and last characters are the same. The first character is 'e' and the last character is 'e', so it's circular.
Input: sentence = "Leetcode is cool"
Output: false
Explanation: The last character of "Leetcode" is 'e', which is not equal to the first character of "is". So it's not circular.
Constraints
1 <= sentence.length <= 500
sentence consists of only lowercase and uppercase English letters and spaces.
sentence has at least one word.
sentence has no leading or trailing spaces.
All the words in sentence are separated by a single space.
Split and Compare
Intuition Split the sentence into words and compare the last character of each word with the first character of the next word, using modulo to handle the circular wrap-around.
Steps
- Split the sentence by spaces to get an array of words
- Iterate through each word and compare its last character with the first character of the next word
- Use modulo operation to wrap around from the last word to the first word
- Return false if any mismatch is found, otherwise return true
class Solution:
def isCircularSentence(self, sentence: str) -> bool:
words = sentence.split()
n = len(words)
for i in range(n):
last_char = words[i][-1]
first_char = words[(i + 1) % n][0]
if last_char != first_char:
return False
return TrueComplexity
- Time: O(n) where n is the length of the sentence
- Space: O(n) for storing the split words array
- Notes: Simple and readable approach but uses extra space for the words array
Single Pass Without Splitting
Intuition Iterate through the string once, checking character boundaries at spaces and the circular condition between the first and last characters.
Steps
- First check if the first character equals the last character (circular condition)
- Iterate through the string and whenever a space is found, verify that the character before the space equals the character after the space
- Return false if any check fails, otherwise return true
class Solution:
def isCircularSentence(self, sentence: str) -> bool:
n = len(sentence)
# Check circular condition: last char == first char
if sentence[0] != sentence[-1]:
return False
# Check each space-separated boundary
for i in range(n):
if sentence[i] == ' ':
if sentence[i - 1] != sentence[i + 1]:
return False
return TrueComplexity
- Time: O(n) where n is the length of the sentence
- Space: O(1) constant extra space
- Notes: Optimal space complexity as we don’t need to store the words array