|
| 1 | +# Longest Happy String |
| 2 | + |
| 3 | +A string is considered happy if it meets the following conditions: |
| 4 | + |
| 5 | +1. It comprises only the characters 'a', 'b', and 'c'. |
| 6 | +2. It does not contain the substrings "aaa", "bbb", or "ccc". |
| 7 | +3. The total occurrences of: |
| 8 | + - The character 'a' does not exceed a. |
| 9 | + - The character 'b' does not exceed b. |
| 10 | + - The character 'c' does not exceed c. |
| 11 | + |
| 12 | +You are given three integers, a, b, and c, representing the maximum allowable occurrences of 'a', 'b', and 'c', |
| 13 | +respectively. Your task is to return the longest possible happy string. If there are multiple valid longest happy |
| 14 | +strings, return any one of them. If no such string can be formed, return an empty string "". |
| 15 | + |
| 16 | +> Note: A substring is a contiguous sequence of characters within a string. |
| 17 | +
|
| 18 | +**Constraints** |
| 19 | + |
| 20 | +- 0 ≤ `a`, `b`, `c` ≤ 100 |
| 21 | +- a + b + c > 0 |
| 22 | + |
| 23 | +## Examples |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +The essence of this solution lies in constructing the longest happy string while adhering to the constraints of avoiding three consecutive identical characters. The approach uses the heaps pattern and prioritizes characters with the highest remaining frequency to maximize the length of the resulting string. A max heap keeps track of the most frequent characters to achieve this. The character with the highest frequency is added to the resulting string at each step, provided it does not violate the constraints. If adding the character would result in three consecutive identical characters, the next most frequent character is selected and added to the resulting string. By carefully managing character counts and maintaining priority with the heap, the solution ensures that valid characters are used to their maximum potential. This process continues until no valid characters can be added, resulting in the longest possible happy string or an empty string if no such string can be formed. |
| 33 | + |
| 34 | +Now, let’s look at the solution steps below: |
| 35 | + |
| 36 | +We initialize a max heap, pq, to store the input frequencies of 'a', 'b', and 'c'. We push the frequencies into pq, where each heap element is a tuple of the frequency and the corresponding character. |
| 37 | + |
| 38 | +We initialize an empty list result to construct the happy string step by step. |
| 39 | + |
| 40 | +We iterate until the heap, pq, is empty and perform the following: |
| 41 | + |
| 42 | +We pop the character with the highest frequency from pq. |
| 43 | + |
| 44 | +We handle repetition constraints by checking if the last two characters in the result are the same as the current character because adding it would violate the rule of avoiding three consecutive identical characters: |
| 45 | + |
| 46 | +In this case, check if another character is available in pq. If yes, pop it, append it to the result, and push it back into pq after decrementing its count. |
| 47 | + |
| 48 | +Push the original character back into the heap to try adding it later. |
| 49 | + |
| 50 | +Otherwise, if adding the current character does not violate the constraints, append it to the result, decrease its count by 1, and push it back into pq if there are remaining occurrences. |
| 51 | + |
| 52 | +Finally, we return the result as the longest happy string. |
| 53 | + |
| 54 | +Note: In Python, strings are immutable, so we construct the result as a list and convert it to a string before returning. |
| 55 | + |
| 56 | +Let’s look at the following illustration to get a better understanding of the solution: |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +### Time complexity |
| 75 | + |
| 76 | +The solution uses a max heap to select characters based on their frequency. Let’s break it down: |
| 77 | + |
| 78 | +1. Adding up to 3 elements ('a', 'b', 'c') to the heap takes O(log3), which simplifies to O(1). |
| 79 | +2. A character is popped from the heap at each step, processed, and potentially pushed back. For a total of k = a + b + c characters, each heap operation takes O(log3) simplifying to O(1), as the heap size is always at most 3. Therefore, the total time for heap operations is O(k⋅1)=O(k). |
| 80 | +3. Building the result string by appending characters takes O(k). |
| 81 | + |
| 82 | +Combining these steps, the overall time complexity of the solution is O(k), where k=a+b+c. |
| 83 | + |
| 84 | +### Space complexity |
| 85 | + |
| 86 | +The heap stores at most three elements ('a', 'b', 'c'), requiring O(1) space, and the result string for the output requires O(k) space, where k=a+b+c. |
| 87 | + |
| 88 | +The space required by the result string for the output is not counted in the solution space. Therefore, the overall space complexity is O(1). |
0 commit comments