Skip to content

Commit 9580d01

Browse files
committed
Longest Substring Without Repeating Characters
1 parent e3d8971 commit 9580d01

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//lint:file-ignore U1000 Ignore all unused code
2+
package main
3+
4+
func lengthOfLongestSubstring(s string) int {
5+
maxLen := 0
6+
head := 0
7+
m := make(map[rune]struct{})
8+
for tail := 1; tail <= len(s); tail++ {
9+
if _, ok := m[rune(s[tail-1])]; ok {
10+
for s[head] != s[tail-1] {
11+
delete(m, rune(s[head]))
12+
head++
13+
}
14+
head++
15+
} else {
16+
m[rune(s[tail-1])] = struct{}{}
17+
}
18+
substring := s[head:tail]
19+
maxLen = max(maxLen, len(substring))
20+
}
21+
return maxLen
22+
}

0 commit comments

Comments
 (0)