LeetCode Entry
2490. Circular Sentence
Are words circular?
2490. Circular Sentence easy
blog post
substack
youtube
deep-dive

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/788
Problem TLDR
Are words circular? #easy
Intuition
If the current char is space, check its surroundings. Don’t forget to check the first and the last letter of the entire sentence (that was what I forgot)
Approach
- let’s do codegolf
- windows() is nice
- regex is slow (and a separate kind of language, but powerful if mastered)
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun isCircularSentence(sentence: String) =
sentence[0] == sentence.last() &&
sentence.windowed(3).all { it[1] != ' ' || it[0] == it[2] }
pub fn is_circular_sentence(sentence: String) -> bool {
let bs = sentence.as_bytes();
bs[0] == bs[bs.len() - 1] && bs.windows(3)
.all(|w| w[1] != b' ' || w[0] == w[2])
}
bool isCircularSentence(string sentence) {
return sentence[0] == sentence.back() &&
!regex_search(sentence, regex("(.) (?!\\1)"));
}