Skip to content

Commit 26f10d0

Browse files
authored
Merge pull request ashutosh97#194 from shagunarora/shagunarora
Generate Parenthesis
2 parents d3628cc + 2d1f4d2 commit 26f10d0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
void Generate_Parenthesis(int open, int close, vector<string> &ans, string s)
4+
{
5+
if (close == 0)
6+
{
7+
ans.push_back(s);
8+
return;
9+
}
10+
if (open)
11+
{
12+
string temp = s;
13+
temp += '(';
14+
Generate_Parenthesis(open - 1, close, ans, temp);
15+
}
16+
if (close > open)
17+
{
18+
string temp = s;
19+
temp += ')';
20+
Generate_Parenthesis(open, close - 1, ans, temp);
21+
}
22+
}
23+
int main()
24+
{
25+
int n;
26+
cin >> n;
27+
vector<string> ans;
28+
Generate_Parenthesis(n, n, ans, "");
29+
for (int i = 0; i < ans.size(); i++)
30+
cout << ans[i] << endl;
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
2+
3+
Sample Input
4+
n = 3
5+
6+
Sample Output
7+
["((()))","(()())","(())()","()(())","()()()"]

0 commit comments

Comments
 (0)