you are given a string ‘s’ ,and return the max length and no repeat character substring of it
- key info
- simulation
- go
- sliding window
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
| func lengthOfLongestSubstring(s string) int { helpMap := make(map[rune]int) firstPoint := 0 secondPoint := 0 runes := []rune(s) maxLength := 0 for firstPoint < len(runes) { firstRune := runes[firstPoint] value, ok := helpMap[firstRune] if ok { helpMap[firstRune] = value + 1 if helpMap[firstRune] > 1 { if firstPoint-secondPoint > maxLength { maxLength = firstPoint - secondPoint } for secondPoint <= firstPoint { secondRune := runes[secondPoint] _, ok := helpMap[secondRune] if ok { helpMap[secondRune] = helpMap[secondRune] - 1 } secondPoint = secondPoint + 1 if helpMap[firstRune] <= 1 { break } } } } else { helpMap[firstRune] = 1 } firstPoint = firstPoint + 1 } if firstPoint-secondPoint > maxLength { maxLength = firstPoint - secondPoint } return maxLength }
|