← Home

482. License Key Formatting

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

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 482. License Key Formatting, 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 licenseKeyFormatting.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • 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//Runtime: 440 ms, faster than 8.71% of C++ online submissions for License Key Formatting.
02//Memory Usage: 10 MB, less than 100.00% of C++ online submissions for License Key Formatting.
03
04class Solution {
05public:
06    string licenseKeyFormatting(string S, int K) {
07        //to upper
08        transform(S.begin(), S.end(), S.begin(), [](unsigned char c){return toupper(c);});
09        
10        //remove all '-'
11        S.erase(remove_if(S.begin(), S.end(),
12                          [](char c) { return c == '-';}),
13                S.end());
14        
15        //start from K pos before S's end
16        //end before S's start
17        for(int i = S.size()-K; i > 0; i-=K){
18            S.insert(S.begin()+i, '-');
19        }
20        
21        return S;
22    }
23};
24
25//https://leetcode.com/problems/license-key-formatting/discuss/96512/Java-5-lines-clean-solution
26//Runtime: 12 ms, faster than 77.39% of C++ online submissions for License Key Formatting.
27//Memory Usage: 10.4 MB, less than 82.76% of C++ online submissions for License Key Formatting.
28class Solution {
29public:
30    string licenseKeyFormatting(string S, int K) {
31        string ans = "";
32
33        for (int i = S.size() - 1; i >= 0; i--){
34            if (S[i] != '-'){
35                //need a '-' so that ans.size() is multiple of (K+1)
36                if(ans.size() % (K + 1) == K) ans += '-';
37                ans += S[i];
38            }
39        }
40        
41        reverse(ans.begin(), ans.end());
42        transform(ans.begin(), ans.end(), ans.begin(), [](unsigned char c){return toupper(c);});
43
44        return ans;
45    }
46};

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.