-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC_balancedParanthesis.cpp
More file actions
executable file
·49 lines (48 loc) · 1.05 KB
/
LC_balancedParanthesis.cpp
File metadata and controls
executable file
·49 lines (48 loc) · 1.05 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lli long long int
#define vi vector<int >
#define vvi vector<vector <int >>
#define pb push_back
#define pob pop_back
// Always remember Overflow
// Using Backtracking
vector<string > valid;
void generatePar(string &s, int open, int close){
// cout <<" S: " << s << endl;
if(open == 0 && close == 0){
// cout <<"E1: " << s << endl;
valid.push_back(s);
// cout <<"E2: " << s << endl;
return;
}
if(open > 0){
s.push_back('(');
// cout << "A : " << s << endl;
generatePar(s, open-1, close);
// cout << "B : " << s << endl;
s.pop_back();
// cout << "C : " << s << endl;
}
if(close > 0){
if( open < close ){
s.push_back(')');
// cout << "D : " << s << endl;
generatePar ( s, open, close-1 );
// cout << "E : " << s << endl;
s.pop_back();
// cout << "F : " << s << endl;
}
}
}
int main(){
int n; cin >> n;
string s = "";
generatePar(s, n, n);
for (int i = 0; i < valid.size(); ++i)
{
// cout << valid.size() << endl;
cout << valid[i] ;
}
}