Circular Sentence — Solution in Python, Java & C++
Circular Sentence solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings.
Circular Sentence is an easy coding problem on strings, asked in Amazon, Google and Wipro interviews.
- Difficulty: Easy
- Topics: Strings
- Asked at: Amazon, Google, Wipro
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
A sentence is a list of words separated by a single space, with no leading or trailing space.
It is circular when the last character of every word equals the first character of the next word, and the last character of the final word equals the first character of the first word. The comparison is case-sensitive.
Return whether sentence is circular.
Example 1
Input: sentence = "codekairo output tasks sync"
Output: true
Explanation: o→o, t→t, s→s, and `sync` ends in `c` which begins `codekairo`.
Example 2
Input: sentence = "codekairo builds judges"
Output: false
Explanation: `codekairo` ends in `o` but `builds` begins with `b`.
Example 3
Input: sentence = "racecar"
Output: true
Explanation: A single word is circular when its first and last characters match.
Constraints
1 <= sentence.length <= 500sentence consists 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.
Hints for Circular Sentence
Hint 1
Split on spaces, then compare each word's last character with the next word's first.
Hint 2
Use modular indexing so the last word wraps to the first.
Hint 3
A one-word sentence still has to close the loop with itself.
How to solve Circular Sentence
Split into words and check every consecutive pair around the ring, using (i + 1) mod n so the final word is compared with the first.
Approach
- Split
sentenceon single spaces. - For each index
i, compare the last character ofwords[i]with the first ofwords[(i+1) mod n]. - Return false on the first mismatch, true otherwise.
Why it works
The modulo handles the wrap-around without a special case for the last word — which is the part most solutions forget. Splitting can also be avoided entirely: a character at index i that is a space means sentence[i-1] must equal sentence[i+1], and separately the first and last characters must match.
Complexity
- Time —
O(n) - Space —
O(n) for the split, or O(1) scanning in place
Pitfalls
- The wrap-around from the last word back to the first is part of the rule.
- A single word must still satisfy first-equals-last.
- Comparison is case-sensitive:
'a'and'A'do not match.
Reference solution
Python
def isCircularSentence(sentence: str) -> bool:
words = sentence.split(" ")
n = len(words)
return all(words[i][-1] == words[(i + 1) % n][0] for i in range(n))Java
public static boolean isCircularSentence(String sentence) {
String[] words = sentence.split(" ");
int n = words.length;
for (int i = 0; i < n; i++) {
String cur = words[i];
String next = words[(i + 1) % n];
if (cur.charAt(cur.length() - 1) != next.charAt(0)) return false;
}
return true;
}C++
bool isCircularSentence(string sentence) {
vector<string> words;
string cur;
for (char c : sentence) {
if (c == ' ') {
words.push_back(cur);
cur.clear();
} else {
cur += c;
}
}
words.push_back(cur);
int n = (int) words.size();
for (int i = 0; i < n; i++) {
if (words[i].back() != words[(i + 1) % n].front()) return false;
}
return true;
}JavaScript
var isCircularSentence = function(sentence) {
var words = sentence.split(" ");
for (var i = 0; i < words.length; i++) {
var next = words[(i + 1) % words.length];
if (words[i].charAt(words[i].length - 1) !== next.charAt(0)) return false;
}
return true;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings