A string s
is called happy if it satisfies the following conditions:
s
only contains the letters 'a'
, 'b'
, and 'c'
.s
does not contain any of "aaa"
, "bbb"
, or "ccc"
as a substring.s
contains at most a
occurrences of the letter 'a'
.s
contains at most b
occurrences of the letter 'b'
.s
contains at most c
occurrences of the letter 'c'
.Given three integers a
, b
, and c
, return the longest possible happy string. If there are multiple longest happy strings, return any of them. If there is no such string, return the empty string ""
.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: a = 1, b = 1, c = 7 Output: "ccaccbcc" Explanation: "ccbccacc" would also be a correct answer.
Example 2:
Input: a = 7, b = 1, c = 0 Output: "aabaa" Explanation: It is the only correct answer in this case.
Constraints:
0 <= a, b, c <= 100
a + b + c > 0
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to building the longest happy string is like trying every single possible combination of 'a's, 'b's, and 'c's until we find one that satisfies the 'happy' condition (no three consecutive identical characters). We generate every possible string and then check if it's valid.
Here's how the algorithm would work step-by-step:
def longest_happy_string_brute_force(a_count, b_count, c_count):
longest_happy_string = ""
def is_valid(current_string):
if len(current_string) < 3:
return True
return not (current_string[-1] == current_string[-2] == current_string[-3])
def generate(current_string, remaining_a_count, remaining_b_count, remaining_c_count):
nonlocal longest_happy_string
if remaining_a_count < 0 or remaining_b_count < 0 or remaining_c_count < 0:
return
if not is_valid(current_string):
return
if len(current_string) > len(longest_happy_string):
longest_happy_string = current_string
# Attempt to add 'a' to the string
generate(current_string + 'a', remaining_a_count - 1, remaining_b_count, remaining_c_count)
# Attempt to add 'b' to the string
generate(current_string + 'b', remaining_a_count, remaining_b_count - 1, remaining_c_count)
# Attempt to add 'c' to the string
generate(current_string + 'c', remaining_a_count, remaining_b_count, remaining_c_count - 1)
generate("", a_count, b_count, c_count)
return longest_happy_string
The goal is to create the longest possible string with the constraint that we can't have three consecutive identical characters. We achieve this by greedily picking the most frequent character, but being careful not to overuse it and violate the three-in-a-row rule.
Here's how the algorithm would work step-by-step:
def longest_happy_string(a_count, b_count, c_count):
result_string = ""
remaining_counts = [['a', a_count], ['b', b_count], ['c', c_count]]
while True:
remaining_counts.sort(key=lambda item: item[1], reverse=True)
found = False
for i in range(3):
if remaining_counts[i][1] <= 0:
continue
# Prevent three consecutive chars
if len(result_string) >= 2 and result_string[-1] == remaining_counts[i][0] and result_string[-2] == remaining_counts[i][0]:
continue
result_string += remaining_counts[i][0]
remaining_counts[i][1] -= 1
found = True
break
# If we can't add any character, we're done.
if not found:
break
return result_string
Case | How to Handle |
---|---|
a, b, and c are all zero | Return an empty string as no 'happy' string can be constructed. |
One of a, b, or c is significantly larger than the sum of the other two (e.g., a = 100, b = 1, c = 1) | Prioritize the largest count, but ensure no three consecutive characters are the same to avoid violating 'happy' constraints. |
a, b, and c are all equal | Construct the string by alternating the characters as much as possible to ensure all characters are included without exceeding length 2*count. |
The sum of a, b, and c is very large potentially causing memory issues | The algorithm's space complexity scales linearly with the input counts so consider limiting the maximum allowed sum of a, b, and c. |
Two of a, b, and c are zero | Return a string containing only the character associated with the non-zero count, limited to a length of the count itself. |
All three counts are non-zero but very small (e.g., a=1, b=1, c=1) | Ensure correct handling of all permutations as all characters must be present in the output. |
No valid solution exists because the counts are so skewed. | The algorithm handles this gracefully by prioritizing the highest count, and when no character can be appended without violating 'happy' constraints, it terminates and returns the best result generated. |
Integer overflow when computing the length of the string | Ensure that you use a data type capable of holding values equal to the maximum possible sum of a, b, and c (e.g., int64_t in C++, long in Java, etc.) |