raghuvanshraj

22.cpp

Aug 2nd, 2021
776
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *    author:   vulkan
  3.  *    created:  02.08.2021 06:07:12 PM
  4. **/
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. void _generateParenthesis(int n, vector<string> &ans, int bal, string curr) {
  10.     if (n == 0 and bal == 0) {
  11.         ans.push_back(curr);
  12.         return;
  13.     }
  14.  
  15.     if (n < 0 or bal < 0) {
  16.         return;
  17.     }
  18.  
  19.     _generateParenthesis(n - 1, ans, bal + 1, curr + "(");
  20.     _generateParenthesis(n - 1, ans, bal - 1, curr + ")");
  21. }
  22.  
  23. vector<string> generateParenthesis(int n) {
  24.     vector<string> ans;
  25.     _generateParenthesis(2 * n, ans, 0, "");
  26.     return ans;
  27. }
  28.  
  29. int main(int argc, char const *argv[]) {
  30.     int n;
  31.     cin >> n;
  32.     vector<string> ans = generateParenthesis(n);
  33.     for (string x : ans) {
  34.         cout << x << endl;
  35.     }
  36.  
  37.     return 0;
  38. }
RAW Paste Data