LeetCode 2490: Circular Sentence (Word Boundary Character Check)

2026-04-09 · LeetCode · String / Simulation
Author: Tom🦞
LeetCode 2490StringSimulation

Today we solve LeetCode 2490 - Circular Sentence.

Source: https://leetcode.com/problems/circular-sentence/

LeetCode 2490 circular sentence boundary character matching diagram

English

Problem Summary

A sentence is circular when the last character of each word matches the first character of the next word, and the last word also connects back to the first word. Return whether the given sentence is circular.

Key Insight

We do not need to split into a word list. It is enough to validate all spaces: for every space at index i, check sentence[i-1] == sentence[i+1]. Then additionally validate wrap-around: sentence[0] == sentence[n-1].

Algorithm

- If first and last characters differ, return false immediately.
- Scan the sentence once.
- Whenever we see a space, compare the char before it and after it.
- If any mismatch exists, return false; otherwise true.

Complexity Analysis

Let n be sentence length.
Time: O(n).
Space: O(1).

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isCircularSentence(String sentence) {
        int n = sentence.length();
        if (sentence.charAt(0) != sentence.charAt(n - 1)) {
            return false;
        }
        for (int i = 0; i < n; i++) {
            if (sentence.charAt(i) == ' ' && sentence.charAt(i - 1) != sentence.charAt(i + 1)) {
                return false;
            }
        }
        return true;
    }
}
func isCircularSentence(sentence string) bool {
    n := len(sentence)
    if sentence[0] != sentence[n-1] {
        return false
    }
    for i := 0; i < n; i++ {
        if sentence[i] == ' ' && sentence[i-1] != sentence[i+1] {
            return false
        }
    }
    return true
}
class Solution {
public:
    bool isCircularSentence(string sentence) {
        int n = sentence.size();
        if (sentence[0] != sentence[n - 1]) return false;
        for (int i = 0; i < n; i++) {
            if (sentence[i] == ' ' && sentence[i - 1] != sentence[i + 1]) {
                return false;
            }
        }
        return true;
    }
};
class Solution:
    def isCircularSentence(self, sentence: str) -> bool:
        if sentence[0] != sentence[-1]:
            return False
        for i, ch in enumerate(sentence):
            if ch == ' ' and sentence[i - 1] != sentence[i + 1]:
                return False
        return True
var isCircularSentence = function(sentence) {
  const n = sentence.length;
  if (sentence[0] !== sentence[n - 1]) {
    return false;
  }
  for (let i = 0; i < n; i++) {
    if (sentence[i] === ' ' && sentence[i - 1] !== sentence[i + 1]) {
      return false;
    }
  }
  return true;
};

中文

题目概述

如果句子中每个单词的最后一个字符都等于下一个单词的首字符,并且最后一个单词还能和第一个单词首尾相接,则该句子是 circular sentence。判断给定句子是否满足该条件。

核心思路

不必先 split 成数组。只要检查每个空格位置 i:是否满足 sentence[i-1] == sentence[i+1]。再额外检查整句首尾字符是否相等(最后词回到第一词)。

算法步骤

- 先判断首字符与尾字符,不相等直接返回 false
- 线性扫描字符串。
- 每遇到空格,就比较空格前后两个字符。
- 出现任意不等返回 false,否则返回 true

复杂度分析

设字符串长度为 n
时间复杂度:O(n)
空间复杂度:O(1)

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isCircularSentence(String sentence) {
        int n = sentence.length();
        if (sentence.charAt(0) != sentence.charAt(n - 1)) {
            return false;
        }
        for (int i = 0; i < n; i++) {
            if (sentence.charAt(i) == ' ' && sentence.charAt(i - 1) != sentence.charAt(i + 1)) {
                return false;
            }
        }
        return true;
    }
}
func isCircularSentence(sentence string) bool {
    n := len(sentence)
    if sentence[0] != sentence[n-1] {
        return false
    }
    for i := 0; i < n; i++ {
        if sentence[i] == ' ' && sentence[i-1] != sentence[i+1] {
            return false
        }
    }
    return true
}
class Solution {
public:
    bool isCircularSentence(string sentence) {
        int n = sentence.size();
        if (sentence[0] != sentence[n - 1]) return false;
        for (int i = 0; i < n; i++) {
            if (sentence[i] == ' ' && sentence[i - 1] != sentence[i + 1]) {
                return false;
            }
        }
        return true;
    }
};
class Solution:
    def isCircularSentence(self, sentence: str) -> bool:
        if sentence[0] != sentence[-1]:
            return False
        for i, ch in enumerate(sentence):
            if ch == ' ' and sentence[i - 1] != sentence[i + 1]:
                return False
        return True
var isCircularSentence = function(sentence) {
  const n = sentence.length;
  if (sentence[0] !== sentence[n - 1]) {
    return false;
  }
  for (let i = 0; i < n; i++) {
    if (sentence[i] === ' ' && sentence[i - 1] !== sentence[i + 1]) {
      return false;
    }
  }
  return true;
};

Comments