Untitled

Anonymous
kotlin
05/15/2022 3:01 PM
616 B
18
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

}
Editor is loading...