Sponsored
Sponsored
This approach utilizes a max heap (or priority queue) to always try to append the character with the highest remaining count without breaking the "happy" condition. Here’s the detailed plan:
Once you process all characters when the heap is non-empty, the constructed string is your answer.
Time Complexity: O(n log k), where n is the total number of characters (sum of a, b, c) and k is the number of non-zero count characters (at most 3).
Space Complexity: O(1), as we are only storing a fixed number of elements in the heap (at most 3 with their counts).
1from heapq import heappop, heappush
2
3def longestDiverseString(a: int, b: int, c: int) -> str:
4 max_heap =
The function longestDiverseString
uses a max heap to store the characters and their counts as negative values (since Python’s heapq is a min-heap). We always take two checks before appending: If the same character was used twice consecutively before, we opt for the next available character (if any). After appending a character, if there are still instances of this character left, we push it back into the heap with the updated count. This ensures the resulting string is as long as possible and never violates the 'happy' conditions.
This strategy incorporates the concepts of simulated annealing where the decision of which character to append is softly randomized, guided by the need to form a balanced happy string using all the available characters maximally — performing a structured simulation of building the string dynamically and reevaluating choices iteratively.
Time Complexity: O(k! * n), where k = 3 (permutations of distinct characters) and n denotes total character appearances.
Space Complexity: O(1), only a few data structures to hold options.
1#include <string>
2#include <tuple>
3
4std::string generateHappyString(int a, int b, int c, const std::string& pattern) {
5 std::string result;
int counts[3] = {a, b, c};
char chars[3] = {'a', 'b', 'c'};
for (char pat : pattern) {
auto [count, idx] = std::make_tuple(0, -1);
if (result.size() >= 2 && result.back() == pat && result[result.size() - 2] == pat) {
for (int i = 0; i < 3; ++i) {
if (counts[i] > count && chars[i] != pat) {
count = counts[i];
idx = i;
}
}
if (idx == -1) return result;
result += chars[idx];
counts[idx]--;
} else {
for (int i = 0; i < 3; ++i) {
if (counts[i] > count && chars[i] == pat) {
count = counts[i];
idx = i;
}
}
if (idx != -1) {
result += pat;
counts[idx]--;
}
}
}
return result;
}
std::string simulatedAnnealingLongestHappyString(int a, int b, int c) {
std::string patterns[6] = {"abc", "acb", "bac", "bca", "cab", "cba"};
std::string best;
for (const auto& pattern : patterns) {
std::string candidate = generateHappyString(a, b, c, pattern);
if (candidate.size() > best.size()) {
best = candidate;
}
}
return best;
}
The C++ code builds potential "happy" strings by iterating through all possible orders of appending triplet combinations. For each order (swapping item positions within "abc"), potential outcomes are evaluated with respect to happy constraints and count tracking. The code iteratively improves upon choices to achieve maximal string formations.