"Longest Duplicate Substring" hard task No 1044 not accepted improved bruteforce solution
This commit is contained in:
@@ -52,6 +52,47 @@ func LongestDupSubstringReversedBruteforce(s string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func LongestDupSubstring(s string) string {
|
func LongestDupSubstringSmartforce(s string) string {
|
||||||
return LongestDupSubstringBruteforce(s)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,4 +15,6 @@ func TestLongestDupSubstring(t *testing.T) {
|
|||||||
assert.Equal(t, "xx", LongestDupSubstring("xxx"))
|
assert.Equal(t, "xx", LongestDupSubstring("xxx"))
|
||||||
assert.Equal(t, "bananab", LongestDupSubstring("bananabananab"))
|
assert.Equal(t, "bananab", LongestDupSubstring("bananabananab"))
|
||||||
assert.Equal(t, "sfd", LongestDupSubstring("polxtvqlurgobbzbjcjsupskvzmtuveuavrzcxaifmdwqjbuejdasuxcvsupqnucorfawncjqkbakilhiekavhmsjbtlsfydijafxhgrgbsswtnvdquwokcliaxevairergwdtndsibiulqiutkcwsqpzuyypnhiowgvkkdbpkoyvgwyuqcokjnxluamwnuhqjtaossezwxvkpdkqcleqjkelcludtbigxkueupnxjncrbgvdrsdpppqqcopnwadqsxkvlkghfmtvdeygpyxamvxhmmaunxskgnenobvnzizbxwrjeedisrgzykoaidiffzqzsmirxkvfwmtifywamcpxziyjohcudzelgbmdgiaqwnzdbkwbhdioiokaivzlzscshrvbmggdztjuimribrgmdlcctvldbcxwpqnqbkzrcayqdqraobqcgoxxocclasphcjcydpkgkqhrmmxtklpxfxnrgnzczjxanaltnjhwfatgryqhcllemgqcchiagizpdgiqioqyhrpattutmuotrcwtxvdqzzsomofizebbqrgwruydyawsssjkakjpjlzmfhmjetymftxrhilpcgfqyoohyydnodfortlkeoncmfkdraiqydxcodyapjaxumjwczwmthrcecfgsswwtnyqurvggamjbsjdzmcuyefwnsrnhhpusfdsxbsrsrywbccullupdbovnmzluqsfdhqbef"))
|
assert.Equal(t, "sfd", LongestDupSubstring("polxtvqlurgobbzbjcjsupskvzmtuveuavrzcxaifmdwqjbuejdasuxcvsupqnucorfawncjqkbakilhiekavhmsjbtlsfydijafxhgrgbsswtnvdquwokcliaxevairergwdtndsibiulqiutkcwsqpzuyypnhiowgvkkdbpkoyvgwyuqcokjnxluamwnuhqjtaossezwxvkpdkqcleqjkelcludtbigxkueupnxjncrbgvdrsdpppqqcopnwadqsxkvlkghfmtvdeygpyxamvxhmmaunxskgnenobvnzizbxwrjeedisrgzykoaidiffzqzsmirxkvfwmtifywamcpxziyjohcudzelgbmdgiaqwnzdbkwbhdioiokaivzlzscshrvbmggdztjuimribrgmdlcctvldbcxwpqnqbkzrcayqdqraobqcgoxxocclasphcjcydpkgkqhrmmxtklpxfxnrgnzczjxanaltnjhwfatgryqhcllemgqcchiagizpdgiqioqyhrpattutmuotrcwtxvdqzzsomofizebbqrgwruydyawsssjkakjpjlzmfhmjetymftxrhilpcgfqyoohyydnodfortlkeoncmfkdraiqydxcodyapjaxumjwczwmthrcecfgsswwtnyqurvggamjbsjdzmcuyefwnsrnhhpusfdsxbsrsrywbccullupdbovnmzluqsfdhqbef"))
|
||||||
|
assert.Equal(t, "ma", LongestDupSubstring("nnpxouomcofdjuujloanjimymadkuepightrfodmauhrsy"))
|
||||||
|
assert.Equal(t, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", LongestDupSubstring("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user