寻找最长不含有重复字符串的子串
7/31/2022 go
# 寻找最长不含有重复字符串的子串
- abcabcbb -> abc
- bbbbb -> b
- pwwkew-> wke
对于每一个字母x,
- lsatOccurred[x]不存在,或者 < start ===>>> 无需操作
- lsatOccurred[x] >= start ===>>> 更新start
- 更新lsatOccurred[x],更新lastLength
package main
import "fmt"
func lenOfNonRepeatingSubStr(s string) int {
lastOccurred := make(map[byte]int)
start := 0
maxLength := 0
for i, ch := range []byte(s) {
fmt.Println(lastOccurred)
lastI, ok := lastOccurred[ch]
if ok && lastI >= start {
start = lastI + 1
}
if i-start+1 > maxLength {
maxLength = i - start + 1
}
lastOccurred[ch] = i
}
fmt.Println(lastOccurred)
return maxLength
}
func main() {
fmt.Println(
lenOfNonRepeatingSubStr("asxdfgfdv"),
)
fmt.Println(
lenOfNonRepeatingSubStr("aaacedaxxxvvved"),
)
}
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
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