package main func LongestDupSubstringBruteforce(s string) string { strLen := len(s) if strLen < 2 { return "" } res := "" for ln := 1; ln < strLen; ln++ { for i := 0; i <= strLen-ln; i++ { word1 := s[i : i+ln] count := 0 for j := 0; j <= strLen-ln; j++ { word2 := s[j : j+ln] if word1 == word2 { count++ if count > 1 { res = word1 break } } } } } return res } func LongestDupSubstringReversedBruteforce(s string) string { strLen := len(s) if strLen < 2 { return "" } for ln := strLen - 1; ln >= 1; ln-- { for i := strLen - ln; i >= 0; i-- { word1 := s[i : i+ln] count := 0 for j := strLen - ln; j >= 0; j-- { word2 := s[j : j+ln] if word1 == word2 { count++ if count > 1 { return word1 } } } } } return "" } func LongestDupSubstringSmartforce(s string) string { strLen := len(s) if strLen < 2 { return "" } indicesCurrent := []int{} indicesNext := []int{} res := "" for i := 0; i < strLen; i++ { for j := 0; j < strLen; j++ { indicesCurrent = append(indicesCurrent, j) } for ln := 1; ln <= strLen-i; ln++ { word1 := s[i : i+ln] for _, index := range indicesCurrent { if index+ln > strLen { continue } word2 := s[index : index+ln] if (word1 == word2) && (index != i) { if len(res) < len(word1) { res = word1 } indicesNext = append(indicesNext, index) } } indicesCurrent = indicesNext indicesNext = []int{} } } return res } func LongestDupSubstring(s string) string { return LongestDupSubstringSmartforce(s) }