We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9729740 commit 140d603Copy full SHA for 140d603
kotlin/Sliding Windows/LongestSubstringWithUniqueChars.kt
@@ -0,0 +1,21 @@
1
+fun longestSubstringWithUniqueChars(s: String): Int {
2
+ var maxLen = 0
3
+ val hashSet = mutableSetOf<Char>()
4
+ var left = 0
5
+ var right = 0
6
+ while (right < s.length) {
7
+ // If we encounter a duplicate character in the window, shrink
8
+ // the window until it's no longer a duplicate.
9
+ while (s[right] in hashSet) {
10
+ hashSet.remove(s[left])
11
+ left++
12
+ }
13
+ // Once there are no more duplicates in the window, update
14
+ // 'maxLen' if the current window is larger.
15
+ maxLen = maxOf(maxLen, right - left + 1)
16
+ hashSet.add(s[right])
17
+ // Expand the window.
18
+ right++
19
20
+ return maxLen
21
+}
0 commit comments