Matej Focko
5512ebaf6d
URL: https://leetcode.com/problems/circular-sentence/ Signed-off-by: Matej Focko <me@mfocko.xyz>
14 lines
512 B
Kotlin
14 lines
512 B
Kotlin
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
|
|
}
|
|
}
|