LeetCode/kt/circular-sentence.kt

15 lines
512 B
Kotlin
Raw Permalink Normal View History

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