Skip to content

Instantly share code, notes, and snippets.

@odrianoaliveira
Created March 1, 2026 16:09
Show Gist options
  • Select an option

  • Save odrianoaliveira/568d976b35391543932355d49377af7c to your computer and use it in GitHub Desktop.

Select an option

Save odrianoaliveira/568d976b35391543932355d49377af7c to your computer and use it in GitHub Desktop.
package org.example
import kotlin.math.max
fun main() {
val string = "abcaabgfhdbchfythgjgtuitijgjghjfhhdnnslphopoootkjjajhaabxvcgftyyihkjonpm"
val result = findLongestSubstring(string)
println("result: $result")
println("asserted: ${(result == 9)}")
}
fun findLongestSubstring(string: String): Int {
val chars: MutableSet<Char> = mutableSetOf()
var left = 0
var maxLength = 0
for (right in string.withIndex()) {
while (chars.contains(right.value)) {
chars.remove(string[left])
left++
}
chars.add(right.value)
maxLength = max(maxLength, right.index - left + 1)
}
return maxLength
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment