Circular Sentence — Easy Problem & Solution

A sentence is a list of words separated by a single space, with no leading or trailing space.

  • 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 <= 500
  • sentence 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.

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

  1. Split sentence on single spaces.
  2. For each index i, compare the last character of words[i] with the first of words[(i+1) mod n].
  3. 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))

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#, C++, Go, Java, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 282 strings problems · the whole catalogue