-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-paranthesis.cpp
38 lines (36 loc) · 1.05 KB
/
generate-paranthesis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using namespace std;
#include <iostream>
#include <bits/stdc++.h>
#include <string>
//https://leetcode.com/problems/generate-parentheses/description/
class Solution {
public:
void getParanthesis(vector<string> &ans, string possibleAns,int openParan, int closeParan){
if(openParan==0 && closeParan==0){
ans.push_back(possibleAns);
return;
}
if(openParan){
openParan--;
string str1 = possibleAns;
str1 += '(';
getParanthesis(ans,str1,openParan, closeParan);
openParan++;
}
if(closeParan > openParan){
closeParan--;
string str2 = possibleAns;
str2 += ')';
getParanthesis(ans,str2,openParan, closeParan);
closeParan++;
}
}
vector<string> generateParenthesis(int n) {
vector<string> ans;
string possibleAns = "";
int openParan = n;
int closeParan = n;
getParanthesis(ans,possibleAns,openParan, closeParan);
return ans;
}
};