From 2017c2bf10d76819906dc3746dd85a53c15891cd Mon Sep 17 00:00:00 2001 From: Edoardo Cecchinato <72577095+edo-ce@users.noreply.github.com> Date: Fri, 22 Jul 2022 16:55:56 +0200 Subject: [PATCH] Add Kotlin leetcode 3 --- ...st-Substring-Without-Repeating-Characters.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 kotlin/3-Longest-Substring-Without-Repeating-Characters.kt diff --git a/kotlin/3-Longest-Substring-Without-Repeating-Characters.kt b/kotlin/3-Longest-Substring-Without-Repeating-Characters.kt new file mode 100644 index 000000000..cadf83ca5 --- /dev/null +++ b/kotlin/3-Longest-Substring-Without-Repeating-Characters.kt @@ -0,0 +1,17 @@ +class Solution { + fun lengthOfLongestSubstring(s: String): Int { + val hs = HashSet() + var i = 0 + var j = 0 + var maxLength = 0 + while (j < s.length) { + if (hs.contains(s[j])) { + hs.remove(s[i++]) + } else { + hs.add(s[j++]) + maxLength = maxLength.coerceAtLeast(hs.size) + } + } + return maxLength + } +}