diff --git a/Backtracking/Generate_Paranthesis.cpp b/Backtracking/Generate_Paranthesis.cpp new file mode 100644 index 0000000..b3c60df --- /dev/null +++ b/Backtracking/Generate_Paranthesis.cpp @@ -0,0 +1,47 @@ +// Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. +// Example 1: + +// Input: n = 3 +// Output: ["((()))","(()())","(())()","()(())","()()()"] + +// Example 2: + +// Input: n = 1 +// Output: ["()"] + +// Constraints: +// 1 <= n <= 8 + +class Solution { +public: + + vector generateParenthesis(int n) { + vector answer; + string s=""; + generate(s,n,n,answer); + return answer; + } + + void generate(string &s,int open,int close,vector &answer){ + + if(open==0 and close==0) + { + answer.push_back(s); + return; + } + if(open>0) + { + s.push_back('('); + generate(s,open-1,close,answer); + s.pop_back(); + } + if(close>0){ + if(open