diff --git a/C++/2. Backtracking/generate-parenthesis.cpp b/C++/2. Backtracking/generate-parenthesis.cpp new file mode 100644 index 00000000..11316e9e --- /dev/null +++ b/C++/2. Backtracking/generate-parenthesis.cpp @@ -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 +#include +using namespace std; + +void backtrack(vector &res,string curr,int open,int close,int max) { + if(curr.length()==max*2) { + res.push_back(curr); + return; + } + else { + if(open generateParenthesis(int n) { + vector res; + backtrack(res,"",0,0,n); + return res; +} + +int main() { + int n = 3; + vector parenthesis = generateParenthesis(n); + for(auto str : parenthesis) { + cout << str << " "; + } + return 0; +}