Skip to content

Instantly share code, notes, and snippets.

@islishude
Created June 4, 2019 06:40
Show Gist options
  • Save islishude/42b0715207a6f8ff8773a53cc88b64a0 to your computer and use it in GitHub Desktop.
Save islishude/42b0715207a6f8ff8773a53cc88b64a0 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func main() {
fmt.Println(find("abcabcd"))
}
func find(src string) string {
lastChar := make(map[rune]int)
var maxLength, start int
runes := []rune(src)
for charIndex, char := range runes {
if last, ok := lastChar[char]; ok && last >= start {
start = last + 1
}
if length := charIndex - start + 1; length > maxLength {
maxLength = length
}
lastChar[char] = charIndex
}
return string(runes[start : start+maxLength])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment