Skip to content

Commit cbb66ea

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

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

go/longest_substring_without_repeating_characters.go

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

44
func lengthOfLongestSubstring(s string) int {
5+
runeS := []rune(s)
56
maxLen := 0
67
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]))
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])
1213
head++
1314
}
1415
head++
1516
} else {
16-
m[rune(s[tail-1])] = struct{}{}
17+
using[runeS[tail]] = struct{}{}
1718
}
18-
substring := s[head:tail]
19+
substring := runeS[head : tail+1]
1920
maxLen = max(maxLen, len(substring))
2021
}
2122
return maxLen

0 commit comments

Comments
 (0)