2022-08-16 11:10:52 +02:00
|
|
|
class Solution {
|
|
|
|
private data class Accumulator(val firstIndex: Int, var count: Int = 0) {
|
|
|
|
fun add() {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 18:23:38 +02:00
|
|
|
fun firstUniqChar(s: String): Int =
|
|
|
|
s
|
|
|
|
.foldIndexed(mutableMapOf<Char, Accumulator>()) { i, acc, c ->
|
|
|
|
acc.getOrPut(c) { Accumulator(i) }.add()
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
.filterValues { it.count == 1 }
|
|
|
|
.minByOrNull { (_, acc) -> acc.firstIndex }
|
|
|
|
?.value
|
|
|
|
?.firstIndex
|
|
|
|
?: -1
|
2022-08-16 11:10:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun main() {
|
|
|
|
val s = Solution()
|
|
|
|
|
|
|
|
check(s.firstUniqChar("leetcode") == 0)
|
|
|
|
check(s.firstUniqChar("loveleetcode") == 2)
|
|
|
|
check(s.firstUniqChar("aabb") == -1)
|
|
|
|
}
|