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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for the circular sentence problem involves checking if the first letter of each word matches the last letter of the previous word, going all the way around. It makes sure that for every adjacent pair of words, this property holds true. It's like meticulously checking every link in a chain to see if it connects properly.
Here's how the algorithm would work step-by-step:
def is_circular_sentence(sentence):
words = sentence.split()
# Handle edge case of empty sentence
if not words:
return True
number_of_words = len(words)
# Iterate through words checking first and last letters
for i in range(number_of_words - 1):
current_word = words[i]
next_word = words[i + 1]
# Check if the last letter of current word matches the first letter of next word
if current_word[-1] != next_word[0]:
return False
# Check if last word connects back to first word
if words[-1][-1] != words[0][0]:
return False
return TrueThe key is to check if the first letter of each word matches the last letter of the preceding word. If the sentence is made of only one word, verify the first and last letter of the word are the same. This approach quickly verifies the circular property by focusing on adjacent word pairings.
Here's how the algorithm would work step-by-step:
def is_circular_sentence(sentence):
words = sentence.split()
# Handle the base case of a single-word sentence
if len(words) == 1:
return words[0][0] == words[0][-1]
# Check if the last letter of the last word matches the first letter of the first word
if words[-1][-1] != words[0][0]:
return False
# Iterate through the words to check if adjacent words form a cycle
for word_index in range(len(words) - 1):
current_word = words[word_index]
next_word = words[word_index + 1]
# If the last character of the current word
# does not equal the first character of the next
if current_word[-1] != next_word[0]:
return False
#Check that adjacent words connect
return True| Case | How to Handle |
|---|---|
| Null or empty sentence | Return true if the sentence is null or empty, as it vacuously satisfies the circular condition. |
| Sentence with only one word | Return true if the sentence has only one word because the last character is the same as the first. |
| Sentence with two words where the last character of the first word doesn't match the first character of the second word | Return false because the basic circular requirement is not met. |
| Sentence with two words where the last character of the second word doesn't match the first character of the first word | Return false because the basic circular requirement is not met. |
| Sentence with leading/trailing spaces or multiple spaces between words | The solution should trim leading/trailing spaces and split on single spaces only. |
| Sentence with mixed-case letters | The comparison of first and last letters must be case-insensitive. |
| Very long sentence (performance) | The solution should iterate linearly through the words, so long sentences should not cause performance issues. |
| Sentence containing non-alphabetic characters | The problem statement specifies only uppercase and lowercase letters are allowed, so either throw an error or assume that the code must compare alphanumeric characters and skip over non-alphanumeric ones. |