backtracing The idea is intuitive. Use two integers to count the remaining left parenthesis (n) and the right parenthesis (m) to be added. At each function call add a left parenthesis if n > 0 and add a right parenthesis if m>0. Append the result and terminate recursive calls when both m and n are zero. */
| 44 | when both m and n are zero. |
| 45 | */ |
| 46 | vector<string> generateParenthesis(int n) { |
| 47 | vector<string> res; |
| 48 | string s = ""; |
| 49 | this->bt(res,n,n,s); |
| 50 | return res; |
| 51 | } |
| 52 | |
| 53 | void bt(vector<string> & v, int l, int r, string & str) { |
| 54 | if (l == 0 and r == 0) { |