Given a string s
, determine if it is a circular sentence. A sentence is considered circular if:
The string s
consists of lowercase English letters and spaces ''
.
Return True
if s
is a circular sentence. Otherwise, return False
.
def isCircularSentence(s: str) -> bool:
# Split the sentence into words
words = s.split()
# If there's only one word, it's considered circular.
if len(words) == 1:
return True
# Check the circular condition for each word
for i in range(len(words) - 1):
if words[i][-1] != words[i + 1][0]:
return False
# Check the circular condition between the last and the first word
if words[-1][-1] != words[0][0]:
return False
return True
# Example Usage
s = "lazyly am marvelous"
print(isCircularSentence(s)) # Output: True
The time complexity of this solution is O(n):
m
is the number of words. m
is bound by n
since each word has at least one character, so O(m) ≤ O(n).Thus, the overall complexity is O(n), where n
is the length of the string.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?