-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateParenthesis.cpp
88 lines (79 loc) · 2.02 KB
/
GenerateParenthesis.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <bits/stdc++.h>
#include "./utilities.h"
using namespace std;
vector<string> AllParenthesis(int n) ;
// N is the number of pairs of parentheses
// Return list of all combinations of balanced parantheses
class Solution
{
public:
// AC: GFG | AC: LeetCode (Better than 100%)
void findPermutation2(string& str, vector<string>& ans, int n, int open = 0, int close = 0)
{
if(str.length() == 2 * n) {
ans.push_back(str);
return;
}
if(open < n) {
findPermutation2(str.append("("), ans, n, open + 1, close);
str.pop_back();
}
if(close < open) {
findPermutation2(str.append(")"), ans, n, open, close + 1);
str.pop_back();
}
}
vector<string> generateParenthesis(int n) {
vector<string> ans;
string str = "";
findPermutation2(str, ans, n);
return ans;
}
// TLE: GFG | AC: LeetCode (Better than 5.5%)
// TC: O(2^2n * n)
bool isValid(string str) {
int bal = 0;
for(auto it: str) {
if (it == '(')
bal += 1;
else
bal -= 1;
if (bal < 0) return false;
}
return bal == 0;
}
void findPermutation(string& str, vector<string>& ans, int n) {
if(str.length() == 2 * n) {
if(isValid(str)) ans.push_back(str);
return;
}
findPermutation(str.append("("), ans, n);
str.pop_back();
findPermutation(str.append(")"), ans, n);
str.pop_back();
}
vector<string> AllParenthesis(int n) {
vector<string> ans;
string str = "";
findPermutation(str, ans, n);
return ans;
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
Solution ob;
vector<string> result = ob.AllParenthesis(n);
sort(result.begin(),result.end());
for (int i = 0; i < result.size(); ++i)
cout<<result[i]<<"\n";
}
return 0;
}
// } Driver Code Ends