"Longest Duplicate Substring" hard task No 1044 not accepted bruteforce solution

This commit is contained in:
2022-04-20 20:28:18 +03:00
parent 9ebb71d76f
commit f4a90a3f21
2 changed files with 75 additions and 0 deletions

View 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)
}