"Longest Duplicate Substring" hard task No 1044 not accepted improved bruteforce solution
This commit is contained in:
@@ -62,7 +62,7 @@ func LongestDupSubstringSmartforce(s string) string {
|
|||||||
indicesNext := []int{}
|
indicesNext := []int{}
|
||||||
|
|
||||||
res := ""
|
res := ""
|
||||||
for i := 0; i < strLen; i++ {
|
for i := 0; i < strLen-len(res); i++ {
|
||||||
|
|
||||||
for j := 0; j < strLen; j++ {
|
for j := 0; j < strLen; j++ {
|
||||||
indicesCurrent = append(indicesCurrent, j)
|
indicesCurrent = append(indicesCurrent, j)
|
||||||
@@ -93,6 +93,48 @@ func LongestDupSubstringSmartforce(s string) string {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func LongestDupSubstring(s string) string {
|
func LongestDupSubstringSmartforceMark2(s string) string {
|
||||||
return LongestDupSubstringSmartforce(s)
|
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
Reference in New Issue
Block a user