Skip to main content

Circular Sentence - Solution & Explanation

EasyString20 min readAsked at: Bloomberg
Practice this problem

Problem Statement

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. Uppercase and lowercase English letters are considered different.

A sentence is circular if:

  • The last character of each word in the sentence is equal to the first character of its next word.
  • The last character of the last word is equal to the first character of the first word.

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 <= 500
  • sentence consist of only lowercase and uppercase English letters and spaces.
  • The words in sentence are separated by a single space.
  • There are no leading or trailing spaces.

Approach Overview

Problem Overview: A sentence is called circular if the last character of each word matches the first character of the next word, and the last word connects back to the first. Words are separated by spaces. You need to verify this circular property across the entire sentence.

Approach 1: Using a Loop with String Manipulation (Time: O(n), Space: O(1))

The direct way is to split the sentence into words and compare adjacent pairs. First check that the last character of the final word equals the first character of the first word. Then iterate through the list of words and verify that words[i][-1] == words[i+1][0] for every pair. If any comparison fails, the sentence is not circular.

This approach relies entirely on basic string manipulation. Splitting the sentence produces an array of words, and a simple loop checks the circular condition. The runtime is O(n) because each character in the sentence is processed at most once during splitting and comparison. Extra space is technically O(k) for the words array, but it’s usually treated as O(1) relative to the input size since no additional data structures are required.

This version is easy to reason about and works well during interviews when readability matters. The logic mirrors the problem statement directly, which reduces the chance of off‑by‑one mistakes.

Approach 2: Single Pass with Two Pointers (Time: O(n), Space: O(1))

You can avoid splitting the string by scanning the sentence once and checking transitions between words. While iterating through characters, every time you encounter a space, compare the character before it with the character after it. These represent the last letter of the current word and the first letter of the next word.

Two indices (or a simple index scan) act as two pointers to verify boundaries between words. If sentence[i] == ' ', check whether sentence[i-1] == sentence[i+1]. After the loop finishes, perform one more comparison between the last character of the sentence and the first character to complete the circular condition.

This method keeps everything in the original string and avoids creating a words array. The algorithm performs a single traversal of the string, giving O(n) time complexity and O(1) space. It’s slightly more efficient in practice and demonstrates strong control over string indexing.

Recommended for interviews: Both solutions run in O(n) time, but interviewers usually expect the single-pass scan because it avoids unnecessary allocations and shows comfort with string indexing. The split-and-loop method is still valuable—it shows clear reasoning and is often the fastest way to reach a correct answer before optimizing.

Approach 1: Using a Loop with String Manipulation

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time complexity: O(n), where n is the length of the sentence.
Space complexity: O(1), as no additional data structures are used.

Try this approach in the editor →

Approach 2: Using a Single Pass with Two Pointers

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time complexity: O(n), due to the traversal of the sentence.
Space complexity: O(1), as it involves no auxiliary storage.

Try this approach in the editor →

Approach 3: Simulation

We split the string into words by spaces, then check whether the last character of each word is equal to the first character of the next word. If they are not equal, return false. Otherwise, return true after traversing all the words.

The time complexity is O(n), and the space complexity is O(n). Where n is the length of the string.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Try this approach in the editor →

Approach 4: Simulation (Space Optimization)

We can first check whether the first and last characters of the string are equal. If they are not equal, return false. Otherwise, traverse the string. If the current character is a space, check whether the previous character and the next character are equal. If they are not equal, return false. Otherwise, return true after traversing all the characters.

The time complexity is O(n), where n is the length of the string. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using a Loop with String Manipulation

Time complexity: O(n), where n is the length of the sentence.
Space complexity: O(1), as no additional data structures are used.

Using a Single Pass with Two Pointers

Time complexity: O(n), due to the traversal of the sentence.
Space complexity: O(1), as it involves no auxiliary storage.

Simulation
Simulation (Space Optimization)

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Loop with String ManipulationO(n)O(1)Best for clarity and quick implementation using split words
Single Pass with Two PointersO(n)O(1)Preferred in interviews when minimizing extra allocations

Video Solution

Circular Sentence - Leetcode 2490 - PythonNeetCodeIO5,506 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Circular Sentence easy or hard?
Circular Sentence is classified as an Easy problem. The challenge is mainly careful string indexing and handling the circular condition between the last and first words. Most solutions require only a single pass through the sentence.
Circular Sentence Python/Java solution
Python and Java implementations both follow the same idea: check each word boundary and verify matching characters. Either split the sentence into words and compare neighbors or scan the string once and check characters around spaces. Both versions run in O(n) time.
How to solve Circular Sentence in O(n)?
Traverse the sentence once and check every word boundary. When you encounter a space, compare the character before it with the character after it. If they differ, the sentence is not circular. After finishing the scan, compare the last character with the first to complete the circular check.
What is the best approach for Circular Sentence?
The best approach is a single-pass scan of the string using index checks around spaces. Every time a space appears, compare the previous character with the next character to ensure the circular rule holds. Finally compare the last character of the sentence with the first character. This solution runs in O(n) time and O(1) space.
Is Circular Sentence asked at Google/Amazon/Meta?
Circular Sentence is an easy string validation problem commonly seen in coding screens and practice sets. Variants of string traversal and word-boundary checks appear in interviews at companies like Amazon and Google, especially for junior and early-career roles.
What data structure is used in Circular Sentence?
The problem mainly relies on string traversal. Some implementations split the sentence into an array of words, while optimized solutions operate directly on the string with index checks or two-pointer style scanning.
What is the time complexity of Circular Sentence?
The optimal solution runs in O(n) time where n is the length of the sentence. Each character is visited once while verifying word boundaries and character matches. Space complexity is O(1) because the algorithm only uses a few variables without allocating additional structures.

Ready to solve this problem?

Practice Circular Sentence with our built-in code editor and test cases.

Practice on FleetCode