From 1d709125a78fb5a568f31a8f7c31f39ff34ad7c6 Mon Sep 17 00:00:00 2001 From: simran2607 Date: Fri, 1 Oct 2021 17:01:10 +0530 Subject: [PATCH] add cpp program to generate parenthesis --- C++/2. Backtracking/generate-parenthesis.cpp | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 C++/2. Backtracking/generate-parenthesis.cpp 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; +}