kt: add «2490. Circular Sentence»

URL:	https://leetcode.com/problems/circular-sentence/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-11-02 12:59:28 +01:00
parent 6c6b311a4b
commit 5512ebaf6d
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

14
kt/circular-sentence.kt Normal file
View file

@ -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
}
}