12345678910111213141516171819202122232425 |
- package main
- func maxInt(x, y int) int {
- if x > y {
- return x
- }
- return y
- }
- func lengthOfLongestSubstring(s string) int {
- max := 0
- for i := 0; i < len(s); i++ {
- // a map stores all runes showed up after index i
- m := map[byte]int{s[i]: 1}
- for j := i + 1; j < len(s); j++ {
- if m[s[j]] == 1 {
- max = maxInt(len(m), max)
- break
- }
- m[s[j]] = 1
- }
- max = maxInt(len(m), max)
- }
- return max
- }
|