I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1096. Brace Expansion II, 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.
The notes already sitting in the source point us in the right direction:
- not understand
- https://leetcode.com/problems/brace-expansion-ii/discuss/317623/Python3-Clear-and-Short-Recursive-Solution
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are braceExpansionII.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- 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:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- 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
01//not understand
02//https://leetcode.com/problems/brace-expansion-ii/discuss/317623/Python3-Clear-and-Short-Recursive-Solution
03//Runtime: 28 ms, faster than 75.42% of C++ online submissions for Brace Expansion II.
04//Memory Usage: 13.6 MB, less than 13.96% of C++ online submissions for Brace Expansion II.
05class Solution {
06public:
07 vector<string> braceExpansionII(string expression) {
08 vector<vector<vector<string>>> groups = {{}};
09
10 int level = 0;
11 int n = expression.size();
12 int start;
13
14 for(int i = 0; i < n; ++i){
15 char c = expression[i];
16 // cout << i << " " << c << endl;
17 if(c == '{'){
18 if(level == 0){
19 start = i+1;
20 }
21 ++level;
22 }else if(c == '}'){
23 --level;
24 if(level == 0){
25 //recursively process something inbetween first level {}
26 groups.back().push_back(
27 braceExpansionII(
28 expression.substr(start, i-start)));
29 // cout << "return from recursion: ";
30 // for(string& s : groups.back().back()){
31 // cout << s << " ";
32 // }
33 // cout << endl;
34 }
35 }else if(level == 0){
36 if(c == ','){
37 groups.push_back(vector<vector<string>>());
38 }else{
39 groups.back().push_back({string(1,c)});
40 // cout << "push {" << c << "}" << endl;
41 }
42 }
43 }
44
45 // cout << "groups: " << groups.size() << endl;
46
47 set<string> wordset;
48
49 for(auto group : groups){
50 // cout << "group: " << group.size() << endl;
51 while(group.size() > 1){
52 vector<string> last = group.back(); group.erase(group.end()-1);
53 vector<string> sec_last = group.back(); group.erase(group.end()-1);
54 vector<string> tmp;
55 for(string& se : sec_last){
56 for(string& le : last){
57 tmp.push_back(se+le);
58 }
59 }
60 group.push_back(tmp);
61 }
62
63 vector<string> first = *group.begin();
64
65 for(string& s : first){
66 wordset.insert(s);
67 }
68 }
69
70 // cout << "wordset: " << wordset.size() << endl;
71
72 return vector<string>(wordset.begin(), wordset.end());
73 }
74};
Cost