Advertisement
imashutosh51

Generate all valid parenthesis of length k

Jul 23rd, 2022 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. void fun(int n,string cur,int open,int close,vector <string> &ans){
  2.     if(open>n || close>n) return;
  3.     if(open==n && close==n){ ans.push_back(cur); return;}
  4.     if(open==close){
  5.         fun(n,cur+"(",open+1,close,ans);
  6.     }
  7.     else if(open>close){
  8.         fun(n,cur+"(",open+1,close,ans);
  9.         fun(n,cur+")",open,close+1,ans);
  10.     }
  11. }
  12. class Solution
  13. {
  14.     public:
  15.     vector<string> AllParenthesis(int n)
  16.     {
  17.         vector <string> ans;
  18.         fun(n,"",0,0,ans);
  19.         return ans;
  20.     }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement