← Home

1417. Reformat The String

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

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1417. Reformat The String, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. 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.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are reformat.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • 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. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

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//Runtime: 12 ms, faster than 95.94% of C++ online submissions for Reformat The String.
02//Memory Usage: 7.7 MB, less than 100.00% of C++ online submissions for Reformat The String.
03class Solution {
04public:
05    string reformat(string s) {
06        string num = "", alpha = "";
07        
08        for(char c : s){
09            if(c >= '0' && c <= '9'){
10                num += c;
11            }else{
12                alpha += c;
13            }
14        }
15        
16        if(abs((int)num.size() - (int)alpha.size()) >= 2) return "";
17        
18        string ans = "";
19        
20        if(num.size() >= alpha.size()){
21            //num first
22            for(int i = 0; i < num.size(); i++){
23                ans += num[i];
24                if(i < alpha.size()) ans += alpha[i];
25            }
26        }else{
27            //alpha first
28            for(int i = 0; i < alpha.size(); i++){
29                ans += alpha[i];
30                if(i < num.size()) ans += num[i];
31            }
32        }
33         
34           
35        return ans;
36    }
37};

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.