-
Notifications
You must be signed in to change notification settings - Fork 0
/
0003_long_cha.go
39 lines (35 loc) · 723 Bytes
/
0003_long_cha.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
package leetcode
func LongCharacter(s string) int {
lastOccurred := make(map[byte]int)
start := 0
maxLength := 0
for i, ch := range []byte(s) {
if lastL, ok := lastOccurred[ch]; ok && lastL >= start {
start = lastL + 1
}
if i-start+1 > maxLength {
maxLength = i - start + 1
}
lastOccurred[ch] = i
}
return maxLength
}
func lengthOfLongestSubstring(s string) int {
start, maxLen := 0, 0
charMap := make(map[rune]int)
for end, char := range s {
if _, ok := charMap[char]; ok {
for s[start] != byte(char) {
delete(charMap, rune(s[start]))
start++
}
start++
} else {
charMap[char] = end
if end-start+1 > maxLen {
maxLen = end - start + 1
}
}
}
return maxLen
}