You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
480 B
31 lines
480 B
package norepeat
|
|
|
|
func max(x, y int) int {
|
|
if x > y {
|
|
return x
|
|
}
|
|
return y
|
|
}
|
|
|
|
func lengthOfLongestSubstring(s string) int {
|
|
positions := map[byte]int{}
|
|
longest := 0
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
char := s[i]
|
|
pos, exists := positions[char]
|
|
positions[char] = i
|
|
longest = max(longest, len(positions))
|
|
if exists {
|
|
positions = map[byte]int{}
|
|
i = pos
|
|
}
|
|
}
|
|
|
|
return longest
|
|
}
|
|
|
|
func LengthOfLongestSubstring(s string) int {
|
|
return lengthOfLongestSubstring(s)
|
|
}
|