From 5512ebaf6df988e6909171f8ab04fd449f7c2159 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 2 Nov 2024 12:59:28 +0100 Subject: [PATCH] =?UTF-8?q?kt:=20add=20=C2=AB2490.=20Circular=20Sentence?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/circular-sentence/ Signed-off-by: Matej Focko --- kt/circular-sentence.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 kt/circular-sentence.kt diff --git a/kt/circular-sentence.kt b/kt/circular-sentence.kt new file mode 100644 index 0000000..6e858c9 --- /dev/null +++ b/kt/circular-sentence.kt @@ -0,0 +1,14 @@ +class Solution { + private data class PartialResult(val last: Character, val circular: Boolean) { + fun update(word: String): PartialResult = PartialResult(word[word.length - 1], circular && word[0] == last) + } + + fun isCircularSentence(sentence: String): Boolean = + sentence.splitToSequence(" ") + .scan(PartialResult(sentence[sentence.length - 1], true)) { acc, word -> + acc.update(word) + } + .all { + it.circular + } +}