Skip to content

Commit

Permalink
fix khushal87 diameter.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed May 13, 2021
1 parent e5b9540 commit f317faa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Backtracking/generate_all_permutations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <bits/stdc++.h>
using namespace std;

void permute(string s, int l, int r)
{
if (l == r)
{
cout << s << endl;
}
else
{
for (int i = l; i <= r; i++)
{
swap(s[l], s[i]);
permute(s, l + 1, r);
swap(s[l], s[i]);
}
}
}
29 changes: 29 additions & 0 deletions Backtracking/generate_paranthesis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;

void answer(int n, int op, int cl, string res, vector<string> &ans)
{
if (op + cl == 2 * n)
{
ans.push_back(res);
}
if (op < n)
{
answer(n, op + 1, cl, res + "(", ans);
}
if (cl < op)
{
answer(n, op, cl + 1, res + ")", ans);
}
}

vector<string> generateParenthesis(int n)
{
vector<string> ans;
string res = "";
if (n > 0)
{
answer(n, 0, 0, res, ans);
}
return ans;
}

0 comments on commit f317faa

Please sign in to comment.