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:
parent
6c6b311a4b
commit
5512ebaf6d
1 changed files with 14 additions and 0 deletions
14
kt/circular-sentence.kt
Normal file
14
kt/circular-sentence.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue