func longestDiverseString(a int, b int, c int) string {
if a >= b && a >= c {
return helper("a", "b", "c", a, b, c)
} else if b >= a && b >= c {
return helper("b", "a", "c", b, a, c)
} else {
return helper("c", "b", "a", c, b, a)
}
}
func helper(A, B, C string, a, b, c int) string {
res := ""
for a > 1 && (b > 0 || c > 0) {
if a > b + c {
res += A + A
a -= 2
} else {
res += A
a -= 1
}
if b >= c {
res += B
b -= 1
} else {
res += C
c -= 1
}
}
for i := 0; i < 2 && a > 0; i += 1 {
res += A
a -= 1
}
for b > 0 || c > 0 {
if b > 0 {
res += B
b -= 1
}
if c > 0 {
res += C
c -= 1
}
}
return res
}
[LeetCode][Go] LeetCode 1405. Longest Happy String
最新推荐文章于 2024-10-14 17:32:24 发布