Created
March 1, 2026 16:09
-
-
Save odrianoaliveira/568d976b35391543932355d49377af7c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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