From 7de2ef26ff4eb933de1d0543db4e3324586677ff Mon Sep 17 00:00:00 2001 From: Rostislav Kuratch Date: Thu, 21 Apr 2022 20:33:55 +0300 Subject: [PATCH] "Longest Duplicate Substring" hard task No 1044 not accepted improved bruteforce solution --- 1044-longest-duplicate-substring.go | 45 ++++++++++++++++++++++-- 1044-longest-duplicate-substring_test.go | 2 ++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/1044-longest-duplicate-substring.go b/1044-longest-duplicate-substring.go index af40257..22853c0 100644 --- a/1044-longest-duplicate-substring.go +++ b/1044-longest-duplicate-substring.go @@ -52,6 +52,47 @@ func LongestDupSubstringReversedBruteforce(s string) string { return "" } -func LongestDupSubstring(s string) string { - return LongestDupSubstringBruteforce(s) +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) } diff --git a/1044-longest-duplicate-substring_test.go b/1044-longest-duplicate-substring_test.go index 29eee98..e7693fa 100644 --- a/1044-longest-duplicate-substring_test.go +++ b/1044-longest-duplicate-substring_test.go @@ -15,4 +15,6 @@ func TestLongestDupSubstring(t *testing.T) { assert.Equal(t, "xx", LongestDupSubstring("xxx")) assert.Equal(t, "bananab", LongestDupSubstring("bananabananab")) assert.Equal(t, "sfd", LongestDupSubstring("polxtvqlurgobbzbjcjsupskvzmtuveuavrzcxaifmdwqjbuejdasuxcvsupqnucorfawncjqkbakilhiekavhmsjbtlsfydijafxhgrgbsswtnvdquwokcliaxevairergwdtndsibiulqiutkcwsqpzuyypnhiowgvkkdbpkoyvgwyuqcokjnxluamwnuhqjtaossezwxvkpdkqcleqjkelcludtbigxkueupnxjncrbgvdrsdpppqqcopnwadqsxkvlkghfmtvdeygpyxamvxhmmaunxskgnenobvnzizbxwrjeedisrgzykoaidiffzqzsmirxkvfwmtifywamcpxziyjohcudzelgbmdgiaqwnzdbkwbhdioiokaivzlzscshrvbmggdztjuimribrgmdlcctvldbcxwpqnqbkzrcayqdqraobqcgoxxocclasphcjcydpkgkqhrmmxtklpxfxnrgnzczjxanaltnjhwfatgryqhcllemgqcchiagizpdgiqioqyhrpattutmuotrcwtxvdqzzsomofizebbqrgwruydyawsssjkakjpjlzmfhmjetymftxrhilpcgfqyoohyydnodfortlkeoncmfkdraiqydxcodyapjaxumjwczwmthrcecfgsswwtnyqurvggamjbsjdzmcuyefwnsrnhhpusfdsxbsrsrywbccullupdbovnmzluqsfdhqbef")) + assert.Equal(t, "ma", LongestDupSubstring("nnpxouomcofdjuujloanjimymadkuepightrfodmauhrsy")) + assert.Equal(t, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", LongestDupSubstring("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) }