← Home

1405. Longest Happy String

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
140

Let's make this one less mysterious. For 1405. Longest Happy String, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

The notes already sitting in the source point us in the right direction:

  • https://leetcode.com/problems/longest-happy-string/discuss/564277/C%2B%2BJava-a-greater-b-greater-c

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are longestDiverseString.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//https://leetcode.com/problems/longest-happy-string/discuss/564277/C%2B%2BJava-a-greater-b-greater-c
02//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Longest Happy String.
03//Memory Usage: 8.3 MB, less than 100.00% of C++ online submissions for Longest Happy String.
04class Solution {
05public:
06    string longestDiverseString(int a, int b, int c, char aa = 'a', char bb = 'b', char cc = 'c') {
07        //make sure a >= b >= c
08        if(a < b)
09            return longestDiverseString(b,a,c,bb,aa,cc);
10        if(a < c)
11            return longestDiverseString(c,b,a,cc,bb,aa);
12        if(b < c)
13            return longestDiverseString(a,c,b,aa,cc,bb);
14        //we only have aa now
15        if(b == 0){
16            // cout << a << " " << b << " " << c << endl;
17            string tmp = string(min(2, a), aa);
18            // cout << tmp << endl;
19            return tmp;
20        }
21        /*
22        whether to append bb after two aa,
23        if we have most b in the next time, 
24        then we don't have to append b here,
25        otherwise, a is possible to be appended in next time,
26        so we need to insert b here
27        */
28        int use_b = (a - min(2, a)) >= b ? 1 : 0;
29        string tmp = string(min(2, a), aa) + string(use_b, bb) + longestDiverseString(a-min(2, a),b-use_b,c,aa,bb,cc);
30        // cout << a << " " << b << " " << c << endl;
31        // cout << tmp << endl;
32        return tmp;
33    }
34};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.