Skip to content

Commit f15ffef

Browse files
committed
refactor Longest Substring Without Repeating Characters
1 parent c8dae29 commit f15ffef

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

go/longest_substring_without_repeating_characters.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ package main
33

44
func lengthOfLongestSubstring(s string) int {
55
maxLen, head := 0, 0
6-
inUse := make(map[byte]struct{})
6+
inUse := make(map[byte]bool)
77
for tail := range s {
8-
if _, ok := inUse[s[tail]]; ok {
9-
for s[head] != s[tail] {
10-
delete(inUse, s[head])
11-
head++
12-
}
8+
for inUse[s[tail]] {
9+
delete(inUse, s[head])
1310
head++
14-
} else {
15-
inUse[s[tail]] = struct{}{}
1611
}
12+
inUse[s[tail]] = true
1713
maxLen = max(maxLen, tail-head+1)
1814
}
1915
return maxLen

0 commit comments

Comments
 (0)