I feel that all possible combinations have been generated, only the order is different from the OJ's solution.

The idea is simple. Start from "()". There are two ways to add parentheses:

- Add a pair of parentheses on the outside, so it becomes "(())".
- Add a pair of parentheses on the left or/and (depending on whether they are the same) on the right, so it becomes "()()".

And we iterate the whole process.

```
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> od={"()"};
vector<string> nw;
for(int i=1;i<n;i++){
for(int j=0;j<od.size();j++){
nw.push_back("(" + od[j] + ")");
}
for(int j=0;j<od.size();j++){
string x1 = od[j] + "()";
string x2 = "()" + od[j];
if( x1.compare(x2)!=0 ) nw.push_back(x1);
nw.push_back(x2);
}
od = nw;
nw.clear();
}
return od;
}
};
```