Untitled

mail@pastecode.io avatar
unknown
kotlin
3 years ago
461 B
3
Indexable
import java.lang.Math.max

class Solution {
    fun lengthOfLongestSubstring(source: String): Int =
        source
            .fold(Pair(0, listOf<Char>())) { prev, curr ->
                if (!prev.second.contains(curr))
                    Pair(max(prev.first, prev.second.size + 1), listOf(curr) + prev.second)
                else
                    Pair(prev.first, listOf(curr) + prev.second.takeWhile { it != curr })
            }
            .first

}