class Solution { public: int tot; vector<string> ans; void search(string p , int left , int right){ if(left > tot || right > tot) return; if(left == tot && right == tot){ ans.push_back(p); return; } if(left < right) return; search(p + "(" , left + 1 , right); search(p + ")" , left , right + 1); } vector<string> generateParenthesis(int n) { // Note: The Solution object is instantiated only once and is reused by each test case. tot = n; ans.clear(); search("(" , 1 , 0); return ans; } };
by 1957