原题链接:https://leetcode.cn/problems/shortest-string-that-contains-three-strings/description/
一开始想复杂了,其实就是暴力问题,只不过暴力代码稍微复杂一点
func minimumString(a string, b string, c string) string {
perms := [][]string{{a, b, c}, {a, c, b}, {b, a, c}, {b, c, a}, {c, a, b}, {c, b, a}};
res := ""
for _, perm := range perms {
s := merge(merge(perm[0], perm[1]), perm[2])
if res == "" || len(res) > len(s) || (len(res) == len(s) && s < res) {
res = s
}
}
return res
}
func merge(a, b string) string {
if strings.Contains(a, b) {
return a
}
if strings.Contains(b, a) {
return b
}
len_a := len(a)
len_b := len(b)
n := min(len_a, len_b)
for i := n; i > 0; i -- {
if a[:i] == b[len_b-i:] {
return b + a[i:]
}
}
return b + a
}
func min(a, b int) int {
if a < b {
return a
}
return b
}