"Longest Duplicate Substring" hard task No 1044 not accepted bruteforce solution
This commit is contained in:
57
1044-longest-duplicate-substring.go
Normal file
57
1044-longest-duplicate-substring.go
Normal file
@@ -0,0 +1,57 @@
|
||||
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 LongestDupSubstring(s string) string {
|
||||
return LongestDupSubstringBruteforce(s)
|
||||
}
|
||||
18
1044-longest-duplicate-substring_test.go
Normal file
18
1044-longest-duplicate-substring_test.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLongestDupSubstring(t *testing.T) {
|
||||
assert.Equal(t, "ana", LongestDupSubstring("banana"))
|
||||
assert.Equal(t, "", LongestDupSubstring("abcd"))
|
||||
assert.Equal(t, "aaa", LongestDupSubstring("aaaa"))
|
||||
assert.Equal(t, "", LongestDupSubstring(""))
|
||||
assert.Equal(t, "", LongestDupSubstring("a"))
|
||||
assert.Equal(t, "x", LongestDupSubstring("xx"))
|
||||
assert.Equal(t, "xx", LongestDupSubstring("xxx"))
|
||||
assert.Equal(t, "bananab", LongestDupSubstring("bananabananab"))
|
||||
assert.Equal(t, "sfd", LongestDupSubstring("polxtvqlurgobbzbjcjsupskvzmtuveuavrzcxaifmdwqjbuejdasuxcvsupqnucorfawncjqkbakilhiekavhmsjbtlsfydijafxhgrgbsswtnvdquwokcliaxevairergwdtndsibiulqiutkcwsqpzuyypnhiowgvkkdbpkoyvgwyuqcokjnxluamwnuhqjtaossezwxvkpdkqcleqjkelcludtbigxkueupnxjncrbgvdrsdpppqqcopnwadqsxkvlkghfmtvdeygpyxamvxhmmaunxskgnenobvnzizbxwrjeedisrgzykoaidiffzqzsmirxkvfwmtifywamcpxziyjohcudzelgbmdgiaqwnzdbkwbhdioiokaivzlzscshrvbmggdztjuimribrgmdlcctvldbcxwpqnqbkzrcayqdqraobqcgoxxocclasphcjcydpkgkqhrmmxtklpxfxnrgnzczjxanaltnjhwfatgryqhcllemgqcchiagizpdgiqioqyhrpattutmuotrcwtxvdqzzsomofizebbqrgwruydyawsssjkakjpjlzmfhmjetymftxrhilpcgfqyoohyydnodfortlkeoncmfkdraiqydxcodyapjaxumjwczwmthrcecfgsswwtnyqurvggamjbsjdzmcuyefwnsrnhhpusfdsxbsrsrywbccullupdbovnmzluqsfdhqbef"))
|
||||
}
|
||||
Reference in New Issue
Block a user