id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
0
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n int len = n * 2;\n vector<string> res;\n string path;\n function<void(int, int)> dfs = [&] (int left_c, int right_c) {\n if (left_c + right_c == len) {\n res.push_back(path);\n...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
0
{ "code": "class Solution {\npublic:\n string getStr(vector<char>& vc) {\n string res = \"\";\n for(auto ele: vc) {\n res += ele;\n }\n return res;\n }\n \n void backtrack(int openCnt, int closeCnt, int& n, vector<string>& res, vector<char>& vc) {\n if(openCnt...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
0
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n vector<string> result;\n std::function<void(int, int, string&)> impl;\n impl = [&result, &impl](int leftClose, int leftOpen, string& combination) {\n if (leftClose == 0 && leftOpen == 0) {\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
0
{ "code": "class Solution {\npublic:\n void solve(string op, int open, int close, vector<string> &ans){\n if(open == 0 && close == 0){\n ans.push_back(op);\n return;\n }\n if(open == 0 && close > 0){\n ans.push_back(op + std::string(close, ')'));\n r...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
0
{ "code": "class Solution {\npublic:\n string str = \"\";\n void putParenthesis(vector<string>& res, int open, int close) {\n if(open == 0 && close == 0) {\n res.push_back(str);\n return;\n }\n if(open > 0) {\n str += \"(\";\n putParenthesis(res, o...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
0
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n vector<string> ans;\n function<void(int, int, string)> dfs = [&](int l, int r, string t) {\n if (l > n || r > n || l < r) return;\n if (l == n && r == n) {\n ans.push_back(t);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
0
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n vector<string> ans;\n function<void(int, int, string)> dfs = [&](int l, int r, string t) {\n if (l > n || r > n || l < r) return;\n if (l == n && r == n) {\n ans.push_back(t);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
1
{ "code": "class Solution {\npublic:\n void dfs(int i, int j, string now, char p, int total){\n if(i + j == 2 * total){\n ans.push_back(now + p);\n return;\n }\n if(i < total) dfs(i + 1, j, now + p, '(', total);\n if(i > j) dfs(i, j + 1, now + p, ')', total);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
1
{ "code": "class Solution {\npublic:\n void bt(vector<char>& cur, stack<char>& cur_st, vector<string>& ans, const int n, int lefts) {\n if (cur.size() == 2*n) {\n ans.push_back(string(cur.begin(), cur.end()));\n return;\n }\n\n if (!cur_st.empty()) {\n cur.push...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
2
{ "code": "class Solution {\npublic:\nvector<string> ans;\nvoid func(int n,string s,int op,int cl){\n if(s.length() == 2*n){\n ans.push_back(s);\n return;\n }\n if(op < n){\n \n func(n,s+\"(\",op+1,cl);\n \n }\n if(cl < op){\n \n func(n,s +\")...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
2
{ "code": "class Solution {\npublic:\n vector<string>* sol;\n void solve(string stack, int tar, int ocnt, int ccnt)\n {\n if (ocnt == tar && ccnt == tar) {\n sol->push_back(stack);\n return;\n }\n if (ocnt < tar) {\n solve(stack + '(', tar, ocnt + 1, ccnt...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void generate(int ind, int n,int cnt,int check,string s, vector<string>&ans){\n if(ind==n){\n ans.push_back(s);\n return;\n }\n if(check<n/2){\n s+='(';\n generate(ind+1,n,cnt+1,check+1,s,ans);\n s.po...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\n void solve(int x,string& s,vector<string>&v,int cur){\n if(x==0){\n if(cur==0) v.push_back(s);\n return;\n }\n string s1=s+\"(\";\n string s2=s+\")\";\n if(cur>=0 && x-cur>=1)\n solve(x-1,s1,v,cur+1);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n #define ll long long\n vector<string> ans;\n void helper(ll open, ll close, ll total, string x) {\n if(x.size() == total) {\n ans.push_back(x);\n return;\n }\n \n if(open < total / 2) {\n x += '(';\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\nvector<string> ans;\nvoid func(int n,string s,int op,int cl){\n if(s.length() == 2*n){\n ans.push_back(s);\n return;\n }\n if(op < n){\n s = s + \"(\";\n func(n,s,op+1,cl);\n s.pop_back();\n }\n if(cl < op){\n s...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n // n - число скобок всего\n void generate(int n, int balance, string currParentheses, vector<string>& allParentheses) {\n // balance < 0 - число закрывающих больше чем открывающих - последовательность\n // не корректна\n // balance > n - len(currPare...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\n void generate(vector<string> &ans, int num, int open, int close, string res, char ch)\n {\n if(open == num && close == num)\n {\n for(int i=0; i<ans.size(); i++)\n {\n if (ans[i] == res)\n return;\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\n void generate(int n, int open, int close, string str, vector<string>& list) {\n if(open == n && close == n) {\n list.push_back(str);\n return;\n }\n if(open <= n && close < open) {\n string str1 = str;\n str1....
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string>ans;\n void gen(int left,int right,string s){\n if(left<0 || right<0){\n return;\n }\n if(left == 0 && right == 0){\n ans.push_back(s);\n }\n gen(left-1,right,s+'(');\n if(left<right){\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void helper(int o,int c,int n,vector<string>&ans,string temp){\n if(o<0||c<0){\n return;\n }\n if(o==0&&c==0){\n ans.push_back(temp);\n }else{\n //cout<<temp<<endl;\n string temp1=temp;\n temp1...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void find(int ind,int n , vector<string> &ans, string s,int &cnt1,int &cnt2){\n if(ind==n){\n ans.push_back(s);\n return;\n } \n if(cnt1==n/2){\n s[ind] = ')';\n cnt2++;\n find(ind+1,n,ans,s,cnt...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\n\nstring open(int i){\n string result = \"\";\n while(i>0) {\n result +=\"(\";\n i--;\n }\n return result;\n}\nstring close(int i){\n string result = \"\";\n while(i>0) {\n result +=\")\";\n i--;\n }\n return result;\n}\npublic:\n vec...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void allParanthesis(int n,string str,vector<char> brackets,int count,vector<string> &ans){\n if(str.length() == 2 * n){\n int count = 0;\n //cout<<str<<endl;\n for(int i=0; i< 2 * n; i++){\n if(count < 0) break;\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> ans;\n void allcomb(int opn,int cls,string s,int n){\n if(cls==0){\n ans.push_back(s);\n return;\n }\n if(opn<cls){\n if(opn!=0){\n s[n]='(';\n allcomb(opn-1,cls,s,n+1);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> ans;\n void allcomb(int opn,int cls,string s,int n){\n if(cls==0){\n ans.push_back(s);\n return;\n }\n if(opn>0){\n s[n]='(';\n allcomb(opn-1,cls,s,n+1);\n }\n if(opn<cls){\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\nvector<string> ans;\n\nvoid backtrack(int remaining, string p, int open) {\n\n if(remaining < 0) {\n return;\n }\n\n if(remaining == 0 and open == 0) {\n ans.push_back(p);\n }\n\n if(open > 0) {\n p += \")\";\n backtrack(remaining, p, ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<char> pathenChars{'(', ')'};\n\n vector<string> generatePathens(int n, \n string &buildingPathen, int index = 0) {\n vector<string> results;\n for (int i = 0; i < pathenChars.size(); i++){\n buildingPathen[index] = pathenChars[i];\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\n bool check(string& s){\n stack<char> st;\n for(auto it : s){\n if(it=='(') st.push(it);\n else{\n if(st.size()==0) return false;\n char a = st.top(); st.pop();\n if(it==')' && a!='(') return fa...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\n vector<string> genRecurse(string curr, int remO, int remC) {\n // at any point # opening brackets >= # closing brackets\n if (remC == 0) {\n return vector<string>{curr};\n }\n\n if (remO == remC) {\n return genRecurse(curr + '(', remO ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void generate(int left, int right, string s,vector<string>& answer)\n {\n if (left == 0 && right == 0) {\n answer.push_back(s);\n }\n if (left > right || left < 0 || right < 0) {\n return;\n }\n s.push_back('(');\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\nprivate:\n\n vector<string> result;\n\n bool check(string str){\n int n = str.size();\n stack<char> st;\n for(char it : str){\n if(st.empty()){\n if(it == ')') return false;\n else st.push(it);\n }\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\n\n vector<string> generateParenthesis(int n) {\n vector<string> ans;\n if(n == 0){\n ans.push_back(\"\");\n return ans;\n }\n if(n == 1){\n ans.push_back(\"()\");\n return ans;\n }\n else{\...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\n vector<string> generateSub(int remainingOpen, int remainingClose)\n {\n if (remainingOpen == 0)\n {\n return vector<string>{std::string(remainingClose, ')')};\n }\n\n vector<string> res;\n if (remainingOpen > 0)\n {\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\n private:\n void solve(string bracket,int countop,int countclosing,string ans,vector<string>& anss,int n){\n if(countop == n and countclosing== n){\n anss.push_back(ans+bracket);\n }\n if(countop <= n){\n solve(\"(\",countop+1,countclosing,...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void func(int open,int close,int n,int ind,vector<string> &see,string q){\n if(ind>2*n){\n return ;\n }\n if(ind==(2*n-1)){\n q+=')';\n see.push_back(q);\n return ;\n }\n else if(open>=n){\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "\nclass Solution {\n private:\n void backtracking(int open_cnt,int close_cnt,vector<string>&res,vector<char>stack,int n)\n {\n if(open_cnt == n && close_cnt == n)\n {\n res.push_back(string(stack.begin() , stack.end()));\n }\n if(open...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\n void solve(string output, vector<string> &ans, int n, int o=1, int c=0) {\n if(o==n && c==n) {\n ans.push_back(output);\n return;\n }\n if(o<=n) {\n solve(output+\"(\", ans, n, o+1, c);\n }\n if(c<o) {\n so...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\n void helper(string str,int cnt,int n,vector<string> &ans){\n if(str.size() == n*2){\n\n if(cnt==0) ans.push_back(str);\n return;\n\n }\n\n if(cnt>=0 && cnt<n){\n helper(str+'(',cnt+1,n, ans);\n }\n \n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void generateP(int op, int cl, int n, string s, vector<string>&res){\n if(op == n && cl==n){\n res.push_back(s);\n return;\n }\n if(op <= n )generateP(op+1,cl,n,s+\"(\",res);\n if(op > cl)\n generateP(op, cl+1, n, s+\")...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void recur(string curr, vector<string> &ans, int l, int openCount, int closedCount, int n) {\n if(l==2*n && openCount == closedCount) {\n ans.push_back(curr);\n return;\n }\n if(openCount<=n)\n recur(curr+\"(\", ans, l+1, ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void generateStrings(int pos, string curr, int balance, vector<string>& ret) { \n if (pos == 0) { \n if (balance == 0) ret.push_back(curr);\n return;\n }\n\n generateStrings(pos-1, curr + \"(\", balance + 1, ret);\n if (balanc...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> results = {};\n void append(string s , string a , int n) {\n s += a; // add the new character\n int count = 0;\n for (string::iterator it = s.begin(); it != s.end(); it++)\n {\n if (*it == '(') count++;\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void generateStrings(int pos, string curr, int balance, vector<string>& ret) { \n if (pos == 0) { \n if (balance == 0) ret.push_back(curr);\n return;\n }\n\n generateStrings(pos-1, curr + \"(\", balance + 1, ret);\n if (balanc...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "/*\nn -> ( [n-2] ) or () [n-2]\n\n*/\nclass Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n if(n==1){return {\"()\"};}\n set<string> S;\n vector<string> ret;\n for(int i = 1; i<n; ++i){\n auto s1 = generateParenthesis(i);\n auto s2 = ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> ans;\n map<string, bool> m;\n vector<string> generateParenthesis(int n) {\n findAns(n, 0, \"\");\n return ans;\n }\n\n void findAns(int n, int coda, string s)\n {\n if (coda < 0)\n return;\n if (s.size() == ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n stack<char> st;\n vector<string>v;\n void valid(int l,int r,int n)\n {\n if(l==n&&r==n)\n {\n stack <char> c=st;\n string w=\"\";\n while(!c.empty())\n {\n w=c.top()+w;\n c.pop();...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n vector<string> ans;\n search(n, 0, 0, \"\", ans);\n return ans;\n }\nprivate:\n void search(int n, int open, int close, string s, vector<string>& ans) {\n if (open == close && close == n) {\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void check(string &s, int n, int o, int c, vector<string> &ans){\n if(o==n && c==n){\n ans.push_back(s);\n }\n\n //considering open\n if(o<=n){\n string one=s;\n one.push_back('(');\n check(one,n,o+1,c,an...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void fun(int open, int closed, string temp, vector<string>&ans, int n)\n {\n if(open==n&&closed==n){\n ans.push_back(temp);\n } \n if(open<=n){\n fun(open+1,closed,temp+'(',ans,n);\n\n } \n if(open>closed&&closed<=n){\n fun(ope...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> res;\n void generate(string s, int l, int r, int n){\n cout<<s<<endl;\n if(l<=n)\n generate(s+'(',l+1,r,n);\n if(r<l)\n generate(s+')',l,r+1,n);\n if(l==n && r==n)\n res.push_back(s);\n } \n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> res;\n void generate(string s, int l, int r, int n){\n //cout<<s<<endl;\n if(l<=n)\n generate(s+'(',l+1,r,n);\n if(r<l)\n generate(s+')',l,r+1,n);\n if(l==n && r==n)\n res.push_back(s);\n } \n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n // Function to generate a string from the current stack\n string getString(stack<char>& st) {\n string s = \"\";\n stack<char> temp = st; // Copy the original stack\n \n // Reverse order of the stack to generate the correct string\n vect...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n // Function to generate a string from the current stack\n string getString(stack<char>& st) {\n string s = \"\";\n stack<char> temp = st; // Copy the original stack\n \n // Reverse order of the stack to generate the correct string\n vect...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string>ans;\n unordered_set<string>vis;\n void generate(int n,int l,int r,string x){\n // cout<<x<<' '<<l<<' '<<r<<'\\n';\n if(vis.find(x)==vis.end()){\n vis.insert(x);\n if(l>n or r>n) return;\n if(l==r and l==n) an...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void util(int index,int sum,int n,string s,vector<string>& ans){\n if(index==(2*n)) {\n if(sum==0)\n ans.push_back(s);\n return;\n }\n if(sum<n) {\n s+='(';\n util(index+1,sum+1,n,s,ans);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\nvoid genPar(int n,string temp,vector<string>& ans,int index,int oc,int cc){\n //BC\n if(index == 2*n){\n if(oc==cc){\n ans.push_back(temp);\n }\n return;\n }\n\n temp.push_back('(');\n oc++;\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\nvoid genPar(int n,string temp,vector<string>& ans,int index,int oc,int cc){\n //BC\n if(index == 2*n){\n if(oc==cc){\n ans.push_back(temp);\n }\n return;\n }\n\n temp.push_back('(');\n oc++;\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool validParenthesis(string output) {\n int balance = 0;\n for (char c : output) {\n if (c == '(') {\n balance++;\n } else if (c == ')') {\n balance--;\n }\n if (balance < 0) {\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n int index = 0;\n int count = 0;\n vector<string> res;\n string s;\n backtrack(n, res, s, count, index);\n return res;\n }\n \n void backtrack(int n, vector<string>& res, string s,...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\n vector<string> ans;\n void solve(string s, int cnt, int open) {\n if (cnt == 0) {\n if (open == 0)\n ans.push_back(s);\n return;\n }\n if (open > 0) {\n s += ')';\n solve(s, cnt - 1, open - 1);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> ans;\n\n void f(int depth,int i,int n,string s){\n if(i == n){\n if(depth == 0) ans.push_back(s);\n return;\n }\n\n s.push_back('(');\n f(depth+1,i+1,n,s);\n s.pop_back();\n if(depth){\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector <string> s1{}; \n void check(string& s, int l, int r)\n {\n sort(s.begin(),s.end());\n do\n {\n stack <char> s2;\n for(auto c : s)\n {\n if(c == '(')\n s2.push(c);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void solve(set<string> &st,string s,int count,int n)\n {\n if(s.length()>=n)\n {\n if(count==0)\n st.insert(s);\n return ;\n } \n if(count<0)\n return;\n if(count > 0) \n {\n s += ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "#include<iostream>\n#include<vector>\n\nclass Solution{\npublic:\n std::vector<std::string> generateParenthesis(int n){\n return generateParenthesis(n, '(', \"\", 0, 0);\n }\n\nprivate:\n std::vector<std::string> generateParenthesis(int n, char choice, std::string str, int status, int total...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "#include <stack>\n\nclass Solution {\npublic:\n \n\n void helper(stack<char> s, vector<string> &sol, string curr_str, int i , int n){\n if(i == n){\n for(int j=0; j < s.size(); ++j){\n curr_str.push_back(')');\n }\n sol.push_back(curr_str);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateCombinations(string combi, int a, int b, int n) {\n vector<string> ans1 = {}, ans2 = {}, merged = {};\n if (a == n && b == n) {\n merged.push_back(combi);\n // cout<<combi<<endl;\n } else {\n if (...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateCombinations(string combi, int a, int b, int n) {\n vector<string> ans1 = {}, ans2 = {}, merged = {};\n if (a == n && b == n) {\n merged.push_back(combi);\n // cout<<combi<<endl;\n } else {\n if (...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n return rec(\"\", n, 0, 0);\n }\n\n vector<string> rec(string str, int n, int qtd_opened, int qtd_closed){\n vector<string> ans;\n vector<string> aux;\n\n if(qtd_opened == n && n == qtd_closed)\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generatePathens(int n, \n string &buildingPathen, int index = 0, int openCount = 0) {\n vector<string> results;\n vector<char> chars{ ')'};\n if (openCount < n - index){\n chars.insert(chars.begin(), '(');\n }\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool paren(string x){\n stack<char> s;\n for(int i =0; i<x.size(); i++){\n if(x[i]=='('){\n s.push(x[i]);\n }\n else if(x[i]==')'){\n if(s.empty()) {\n return false;\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool valid(string s) {\n int balance = 0; // Balance counter for open and close parentheses\n for (char c : s) {\n if (c == '(') {\n balance++; // Increase balance for every open parenthesis\n } else {\n balance--; // Decrease bala...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool valid(string s){\n stack<char> st;\n for(auto c:s){\n if(c=='(') st.push(c);\n else{\n if(st.empty() || st.top()!='(') return false;\n st.pop(); \n }\n }\n return st.empty();\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool valid(string s){\n stack<char> st;\n for(auto c:s){\n if(c=='(') st.push(c);\n else{\n if(st.empty() || st.top()!='(') return false;\n st.pop(); \n }\n }\n return st.empty();\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool check(string s){\n stack<char>t;\n for(int i=0;i<s.size();i++){if(s[i]=='('){t.push(s[i]);}else{if(!t.empty()){t.pop();}else{return false;}}} return t.empty();\n } \n vector<string> generateParenthesis(int n) {\n vector<string> ans;\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool check(string s){\n stack<char>t;\n for(int i=0;i<s.size();i++){if(s[i]=='('){t.push(s[i]);}else{if(!t.empty()){t.pop();}else{return false;}}} return t.empty();\n } \n vector<string> generateParenthesis(int n) {\n vector<string> ans;\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n // brute force algorithm\n vector<string> combinations;\n\n // do a BFS using a queue to see possible combinations\n queue<string> q;\n q.push(\"\");\n while(!q.empty())\n {\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n return rec(\"\", n);\n }\n\n vector<string> rec(string str, int n){\n vector<string> ans;\n vector<string> aux;\n int qtd_opened = qtd(str, '(');\n int qtd_closed = qtd(str, ')');\n\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n return recurse(n, 0, \"\", 0);\n }\n\n vector<string> recurse(int n, int open, string curr, int depth) {\n if (depth == 2 * n) {\n if (open == 0) return {curr};\n return {};\n }\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "vector<string> generatePermutations(int n)\n{\n // Create a vector with n trues and n falses (sorted)\n string permutation(n, '(');\n permutation.insert(permutation.end(), n, ')');\n vector<string> permutations;\n // Generate all permutations\n do\n {\n permutations.push_back(pe...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "vector<string> generatePermutations(int n)\n{\n // Create a vector with n trues and n falses (sorted)\n string permutation(n, '(');\n permutation.insert(permutation.end(), n, ')');\n vector<string> permutations;\n // Generate all permutations\n do\n {\n permutations.push_back(pe...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "vector<string> generatePermutations(int n)\n{\n // Create a vector with n trues and n falses (sorted)\n string permutation(n, '(');\n permutation.insert(permutation.end(), n, ')');\n vector<string> permutations;\n // Generate all permutations\n do\n {\n permutations.push_back(pe...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool isValid(string &s)\n {\n stack<char> parantheses;\n\n for (char &c : s)\n {\n if (c == '(') parantheses.push(c);\n else\n {\n if (parantheses.empty()) return false;\n if (parantheses.t...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n std::vector<std::string> temp{};\n temp.push_back(\"\");\n\n for(int i = 0; i < 2 * n; ++i) {\n std::vector<std::string> collect{};\n\n for(auto& str: temp) {\n auto copy =...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n vector<string>res;\n backtrack(n,0,0,\"\",res);\n return res;\n }\nprivate:\n void backtrack(int n,int open,int closed,string curr,vector<string>&res){\n stack<char>st;\n if(open == closed ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "bool check(std::string const & s)\n{\n auto count{0};\n for(auto const & c : s)\n {\n if(c == '(')\n {\n ++count;\n }\n else\n {\n --count;\n if(count < 0)\n return false;\n }\n }\n return count == 0;\n...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n int c_open = n; int c_close = n;\n vector<string>res;\n string op = \"\";\n gen(c_open,c_close,res,op);\n return res;\n }\n void gen(int open,int close,vector<string>&res,string op){\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\n vector<string> ret;\n int max;\npublic:\n void dfs(const string s,int cnt){\n //cout << s <<endl;\n if(s.size() > max || cnt<0 ||cnt > max/2 ){\n return; //invaild\n }\n if(s.size() == max && cnt == 0){\n ret.push_back(s); ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\n vector<string> ret;\n int max;\npublic:\n void dfs(string s,int cnt){\n //cout << s <<endl;\n if(s.size() > max || cnt<0 ||cnt > max/2 ){\n return; //invaild\n }\n if(s.size() == max && cnt == 0){\n ret.push_back(s); \n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\n vector<string> ret;\n int max;\npublic:\n void dfs(string s,int cnt){\n //cout << s <<endl;\n if(s.size() > max || cnt<0 ||cnt > max/2 ){\n return; //invaild\n }\n if(s.size() == max && cnt == 0){\n ret.push_back(s); \n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string>ans;\n\n void solve(int cnt,int n,string s){\n if(cnt>n ||cnt<0 || s.length()>n*2) return;\n\n if(cnt==0 && s.length()==n*2){\n ans.push_back(s);\n return;\n }\n\n solve(cnt+1,n,s+\"(\");\n solve(cnt-1,...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool isValid(string s) {\n stack<char> st;\n bool f = false;\n for (int i = 0; i < s.size();i++){\n if(s[i] == '{' || s[i] == '(' || s[i] == '['){\n f = false;\n if(s[i] == '[' ){\n st.push(']');\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool isvalid(vector<char> v){\n stack<char> st;\n for(int i=0;i<v.size();i++){\n if(v[i]=='('){\n st.push('(');\n }else{\n if(st.empty()){\n return false;\n }\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) { return helper(\"\", 0, 0, n); }\n\n vector<string> helper(string curr, int s, int e, int n) {\n if (s == n && e == n)\n return vector<string>{curr};\n\n vector<string> ans;\n if (s < n)\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n vector<string> generateParenthesis(int n) {\n return dfs(n,0,0,\"\");\n }\n vector<string> dfs(int n, int open, int close, string str)\n {\n if (n == open && n == close)\n return {str};\n \n vector<string> result;\n if (o...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\n vector<string>solve(string s, int n, int o, int e){\n vector<string>res={};\n if(o==e && o==n) {\n res.push_back(s);\n return res;\n }\n\n vector<string>res1,res2;\n if(o<n){\n string s1 = s+\"(\";\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n void solve(vector<string> &ans, stack<char> st,\n int open, int close, int n){\n if(open==close and close==n){\n string op=\"\";\n while(!st.empty()){\n op+=st.top();\n st.pop();\n }\n rev...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n bool isvalid(string s){\n stack<char> st;\n for(char c:s){\n if(c=='(')st.push('(');\n else if(c==')'){\n if(st.empty())return false;\n if(st.top()!='(')return false;\n st.pop();\n }\n...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\npublic:\n\n void recursive(vector<string> &combo, string curr_combo, stack<char> open_brack, int n_open, int n_close)\n {\n if(n_open == 0 && n_close == 0)\n {\n if(open_brack.size() == 0)\n {\n combo.push_back(curr_combo);\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "class Solution {\nprivate:\n bool isValid(std::string pString) {\n int leftCount = 0;\n for (char p : pString) {\n if (p == '(') {\n leftCount++;\n } else {\n leftCount--;\n }\n if (leftCount < 0) {\n ...
22
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
3
{ "code": "// brute force - generating all possible strings of length 2n, and then verifying their validity.\n\n// Time O(2^(2n) * n) We are generating all possible strings of length 2n. At each character, we have two choices: choosing ( or ), which means there are a total of 2^2n\n// unique strings.\n// Space O(2^(2...