-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22.cpp
More file actions
33 lines (28 loc) · 794 Bytes
/
22.cpp
File metadata and controls
33 lines (28 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* Written by Nitin Kumar Maharana
* nitin.maharana@gmail.com
*/
class Solution {
vector<string> result;
void generateParenthesis(int n, int left, int right, int index, string& tempResult)
{
if(!left && !right)
result.push_back(tempResult);
if(left)
{
tempResult[index] = '(';
generateParenthesis(n, left-1, right, index+1, tempResult);
}
if(right > left)
{
tempResult[index] = ')';
generateParenthesis(n, left, right-1, index+1, tempResult);
}
}
public:
vector<string> generateParenthesis(int n) {
string tempResult(2*n, 'X');
generateParenthesis(n, n, n, 0, tempResult);
return result;
}
};