Guest User

generate parenthese.cpp

a guest
Sep 11th, 2019
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void compute(string &s, int n, int open, int close)
  5. {
  6. // Base solution
  7. if(close==n)
  8. {
  9. cout<<s<<endl;
  10. return ;
  11. }
  12. // Recursive cases
  13. if (open>close)
  14. {
  15. s.append("}");
  16. compute(s,n,open,close+1);
  17. s.pop_back();
  18. }
  19. if(open<n)
  20. {
  21. s.append("{");
  22. compute(s,n,open+1,close);
  23. s.pop_back();
  24. }
  25.  
  26. }
  27. int main()
  28. {
  29. int n;
  30. cin>>n;
  31. string s="";
  32. compute(s,n,0,0);
  33. return 0;
  34. }
Add Comment
Please, Sign In to add comment