Skip to content

Commit 544e7e9

Browse files
committed
refactor Longest Substring Without Repeating Characters
1 parent cbb66ea commit 544e7e9

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

go/longest_substring_without_repeating_characters.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
package main
33

44
func lengthOfLongestSubstring(s string) int {
5-
runeS := []rune(s)
6-
maxLen := 0
7-
head := 0
8-
using := make(map[rune]struct{})
9-
for tail := 0; tail < len(runeS); tail++ {
10-
if _, ok := using[runeS[tail]]; ok {
11-
for runeS[head] != runeS[tail] {
12-
delete(using, runeS[head])
5+
maxLen, head := 0, 0
6+
using := make(map[byte]struct{})
7+
for tail := range s {
8+
if _, ok := using[s[tail]]; ok {
9+
for s[head] != s[tail] {
10+
delete(using, s[head])
1311
head++
1412
}
1513
head++
1614
} else {
17-
using[runeS[tail]] = struct{}{}
15+
using[s[tail]] = struct{}{}
1816
}
19-
substring := runeS[head : tail+1]
20-
maxLen = max(maxLen, len(substring))
17+
maxLen = max(maxLen, tail-head+1)
2118
}
2219
return maxLen
2320
}

0 commit comments

Comments
 (0)