← Home

1544. Make The String Great

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
stackC++Markdown
154

Let's make this one less mysterious. For 1544. Make The String Great, the solution in this repository is mainly a stack 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: stack.

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

  • Brute force

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are makeGood.

Guide

Why?

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

  • The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
  • 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//Brute force
02//Runtime: 4 ms, faster than 83.33% of C++ online submissions for Make The String Great.
03//Memory Usage: 6.2 MB, less than 83.33% of C++ online submissions for Make The String Great.
04class Solution {
05public:
06    string makeGood(string s) {
07        int i = 0;
08        
09        while(i+1 < s.size()){
10            //'A': 65, 'a': 97
11            if(abs(s[i] - s[i+1]) == 32){
12                s.erase(i, 2);
13                if(i > 0) --i;
14            }else{
15                ++i;
16            }
17        }
18        
19        return s;
20    }
21};
22
23//stack
24//https://leetcode.com/problems/make-the-string-great/discuss/781009/Java-Simple-Solution-using-Stack-Explained
25//Runtime: 4 ms, faster than 83.33% of C++ online submissions for Make The String Great.
26//Memory Usage: 6.8 MB, less than 33.33% of C++ online submissions for Make The String Great.
27class Solution {
28public:
29    string makeGood(string s) {
30        stack<char> stk;
31        int n = s.size();
32        
33        for(int i = 0; i < n; ++i){
34            if(!stk.empty() && abs(stk.top()-s[i]) == 32){
35                stk.pop();
36            }else{
37                stk.push(s[i]);
38            }
39        }
40        
41        string ans(stk.size(), '#');
42        
43        for(int i = stk.size()-1; i >= 0; --i){
44            ans[i] = stk.top(); stk.pop();
45        }
46        
47        return ans;
48    }
49};

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.