2490. Circular Sentence - Detailed Explanation
Problem Statement
A sentence is given as a string consisting of one or more words separated by single spaces. A sentence is considered circular if the following conditions hold:
-
For every adjacent pair of words in the sentence, the last character of the first word is the same as the first character of the next word.
-
Additionally, the last character of the last word must be the same as the first character of the first word.
The task is to determine if the given sentence is circular.
Detailed Explanation
How to Determine if a Sentence is Circular
-
Splitting the Sentence:
- First, split the input string by spaces to obtain a list of words.
- For example, if the sentence is
"leetcode exercises sound delightful"
, splitting it yields:
["leetcode", "exercises", "sound", "delightful"]
.
-
Checking Adjacent Words:
- For every pair of consecutive words, check if the last character of the current word matches the first character of the next word.
- In our example:
- Check if the last character of
"leetcode"
(which is'e'
) matches the first character of"exercises"
(which is'e'
). - Then, check if the last character of
"exercises"
(which is's'
) matches the first character of"sound"
(which is's'
). - Next, check if the last character of
"sound"
(which is'd'
) matches the first character of"delightful"
(which is'd'
).
- Check if the last character of
-
Circular Check for the First and Last Words:
- After checking all adjacent pairs, verify that the last character of the last word equals the first character of the first word.
- In our example, check if the last character of
"delightful"
(which is'l'
) matches the first character of"leetcode"
(which is'l'
).
-
Return the Result:
- If all these conditions are met, the sentence is circular and the function should return
true
; otherwise, it returnsfalse
.
- If all these conditions are met, the sentence is circular and the function should return
Example Walkthrough
Example 1
- Input:
"leetcode exercises sound delightful"
- Processing:
- Split into words:
["leetcode", "exercises", "sound", "delightful"]
- Check adjacent pairs:
"leetcode"
ends with'e'
,"exercises"
starts with'e'
→ ✓"exercises"
ends with's'
,"sound"
starts with's'
→ ✓"sound"
ends with'd'
,"delightful"
starts with'd'
→ ✓
- Check circular condition:
"delightful"
ends with'l'
,"leetcode"
starts with'l'
→ ✓
- Split into words:
- Output:
true
Example 2
- Input:
"arc code"
- Processing:
- Split into words:
["arc", "code"]
- Check adjacent pair:
"arc"
ends with'c'
,"code"
starts with'c'
→ ✓
- Check circular condition:
"code"
ends with'e'
,"arc"
starts with'a'
→ ✗
- Split into words:
- Output:
false
Approach Summary
-
Split the Sentence:
- Use the split function to get a list of words.
-
Iterate Through Adjacent Words:
- Loop over the words list and compare the last character of the current word with the first character of the next word.
-
Circular Check:
- After the loop, compare the last character of the final word with the first character of the first word.
-
Return Boolean Result:
- If all checks pass, the sentence is circular; otherwise, it is not.
Time and Space Complexity
-
Time Complexity:
O(n), where n is the number of words (or more precisely, the number of characters in the sentence) because we iterate through the words once and perform constant time checks for each pair. -
Space Complexity:
O(n) for storing the split words. The additional space usage is proportional to the number of words.
Python Code
Java Code
Edge Cases
-
Single Word Sentence:
- For a sentence with only one word, the sentence is circular if the word’s first and last characters are the same.
- Example:
"aba"
is circular, but"abc"
is not.
-
Empty Sentence:
- The problem definition implies at least one word exists. However, if an empty string is encountered, the behavior should be defined (typically, it might return
false
or be considered invalid input).
- The problem definition implies at least one word exists. However, if an empty string is encountered, the behavior should be defined (typically, it might return
-
Sentence with Punctuation:
- The problem typically considers only words separated by spaces. If punctuation is present, the problem statement should clarify how to handle it. Often, such characters are treated as part of the word.
Common Mistakes
-
Not Checking the Circular Condition:
- A common oversight is to check only the adjacent pairs and forget to check if the last character of the last word matches the first character of the first word.
-
Incorrect Splitting of the Sentence:
- Assuming multiple spaces or punctuation may require extra handling. The problem usually states that words are separated by a single space.
-
Case Sensitivity Issues:
- If the problem is case-sensitive, ensure that comparisons are done correctly. In many cases, the sentence consists only of lowercase letters.
-
Off-by-One Errors:
- Incorrectly iterating through the list of words or comparing wrong indices (for example, comparing the wrong characters) can lead to errors.
Related Problems
GET YOUR FREE
Coding Questions Catalog