"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)
|
||||
}
|
||||
Reference in New Issue
Block a user