diff --git a/C++/generateparenthesis.cpp b/C++/generateparenthesis.cpp new file mode 100644 index 0000000..a648b9e --- /dev/null +++ b/C++/generateparenthesis.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +void solve(string s, int open, int close, vector&res){ + if (open==0 and close==0){ + res.push_back(s); + return; + } + if (open>0){ + solve(s+"(", open-1, close, res); + } + if (open < close){ + solve(s+")", open, close-1, res); + } +} + +int main(){ + //n denotes the number of opening and closing parenthesis + int n=0; + cin >> n; + + vector res; + solve("", n, n, res); + + for (auto it: res){ + cout << it << endl; + } + return 0; +}