My thought: To confirm to the limitation, we can construct a stack mentally. A '(' corresponds to push, while a ')' corresponds to pop. At this point, our mission is to push all '('s in to the stack and pop the stack until it's empty. Of course, the operations can interleave. I took a recursive way since it's straight forward that at each state, say m '(' waiting to be pushed and n '(' waiting to be popped, we can either push a '(' or pop a '('.

```
public class Solution {
List<String> ans = new ArrayList<String>();
public List<String> generateParenthesis(int n) {
dp(n, 0, "");
return ans;
}
public void dp(int outstack, int instack, String res) {
if(outstack == 0 && instack == 0) {
ans.add(res);
return;
}
if(outstack > 0) {
dp(outstack-1, instack+1, res+"(");
}
if(instack > 0) {
dp(outstack, instack-1, res+")");
}
return;
}
}
```

However, I don't have a clue how to implement it iteratively :(