Created
June 4, 2019 06:40
-
-
Save islishude/42b0715207a6f8ff8773a53cc88b64a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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