3.go 348 B

123456789101112131415161718
  1. package main
  2. func lengthOfLongestSubstring(s string) int {
  3. max := 0
  4. for i := 0; i < len(s); i++ {
  5. // a map stores all runes showed up after index i
  6. m := map[byte]int{s[i]: 1}
  7. for j := i + 1; j < len(s); j++ {
  8. if m[s[j]] == 1 {
  9. max = maxInt(len(m), max)
  10. break
  11. }
  12. m[s[j]] = 1
  13. }
  14. max = maxInt(len(m), max)
  15. }
  16. return max
  17. }