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

This commit is contained in:
2022-04-21 23:40:07 +03:00
parent 7de2ef26ff
commit 4d4cc6758d
2 changed files with 49 additions and 4 deletions

View File

@@ -62,7 +62,7 @@ func LongestDupSubstringSmartforce(s string) string {
indicesNext := []int{}
res := ""
for i := 0; i < strLen; i++ {
for i := 0; i < strLen-len(res); i++ {
for j := 0; j < strLen; j++ {
indicesCurrent = append(indicesCurrent, j)
@@ -93,6 +93,48 @@ func LongestDupSubstringSmartforce(s string) string {
return res
}
func LongestDupSubstring(s string) string {
return LongestDupSubstringSmartforce(s)
func LongestDupSubstringSmartforceMark2(s string) string {
strLen := len(s)
if strLen < 2 {
return ""
}
indicesNext := []int{}
indicesAll := []int{}
for j := 0; j < strLen; j++ {
indicesAll = append(indicesAll, j)
}
res := ""
for i := 0; i < strLen-len(res); i++ {
indicesCurrent := indicesAll
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 (index != i) && (word1 == word2) {
if len(res) < len(word1) {
res = word1
}
indicesNext = append(indicesNext, index)
}
}
indicesCurrent = indicesNext
indicesNext = []int{}
}
}
return res
}
func LongestDupSubstring(s string) string {
return LongestDupSubstringSmartforceMark2(s)
}

File diff suppressed because one or more lines are too long