Skip to content

Commit 1587b94

Browse files
authored
Merge pull request neetcode-gh#587 from edo-ce/main
Add Kotlin 3-Longest-Substring-Without-Repeating-Characters
2 parents 0483b96 + 2017c2b commit 1587b94

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
fun lengthOfLongestSubstring(s: String): Int {
3+
val hs = HashSet<Char>()
4+
var i = 0
5+
var j = 0
6+
var maxLength = 0
7+
while (j < s.length) {
8+
if (hs.contains(s[j])) {
9+
hs.remove(s[i++])
10+
} else {
11+
hs.add(s[j++])
12+
maxLength = maxLength.coerceAtLeast(hs.size)
13+
}
14+
}
15+
return maxLength
16+
}
17+
}

0 commit comments

Comments
 (0)