Generate Parentheses LeetCode solution in C++

1
class Solution {
2
private:
3
void solve(int cnt1, int cnt2, int n, vector<string> &ans, string &op, int ind) {
4
if (ind >= n * 2) {
5
ans.push_back(op);
6
return;
7
}
8
if (cnt1 < n and ind < n * 2 - 1) {
9
op += '(';
10
solve(cnt1 + 1, cnt2, n, ans, op, ind + 1);
11
op.pop_back();
12
}
13

14
if (cnt2 < cnt1 and ind > 0) {
15
op += ')';
16

17
solve(cnt1, cnt2 + 1, n, ans, op, ind + 1);
18
op.pop_back();
19
}
20
}
21

22
public:
23
vector<string> generateParenthesis(int n) {
24
string op = "";
25
vector<string> ans;
26
solve(0, 0, n, ans, op, 0);
27
return ans;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0