16.go 415 B

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