Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions C++/2. Backtracking/generate-parenthesis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
* Input: n = 3
* Output: ["((()))","(()())","(())()","()(())","()()()"]
*/

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

void backtrack(vector<string> &res,string curr,int open,int close,int max) {
if(curr.length()==max*2) {
res.push_back(curr);
return;
}
else {
if(open<max)
backtrack(res,curr+"(",open+1,close,max);
if(close<open)
backtrack(res,curr+")",open,close+1,max);

}
}

vector<string> generateParenthesis(int n) {
vector<string> res;
backtrack(res,"",0,0,n);
return res;
}

int main() {
int n = 3;
vector<string> parenthesis = generateParenthesis(n);
for(auto str : parenthesis) {
cout << str << " ";
}
return 0;
}