-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (36 loc) · 850 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
package main
import "fmt"
func impl1(s string) int {
var (
m = make(map[rune]struct{})
maxLen = 0
nextLen = 0
)
for i := range s {
for _, v := range s[i:] {
if _, ok := m[v]; ok {
m = make(map[rune]struct{}, maxLen)
break
}
nextLen += 1
m[v] = struct{}{}
}
if nextLen > maxLen {
maxLen = nextLen
}
nextLen = 0
}
return maxLen
}
func lengthOfLongestSubstring(s string) int {
return impl1(s)
}
func main() {
fmt.Println(lengthOfLongestSubstring("abcabcbb"), 3)
fmt.Println(lengthOfLongestSubstring("bbbbb"), 1)
fmt.Println(lengthOfLongestSubstring("pwwkew"), 3)
fmt.Println(lengthOfLongestSubstring("aab"), 2)
fmt.Println(lengthOfLongestSubstring("dvdf"), 3)
fmt.Println(lengthOfLongestSubstring(""), 0)
}