-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenerateParentheses.cpp
47 lines (42 loc) · 1.25 KB
/
GenerateParentheses.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
39
40
41
42
43
44
45
46
47
// Generate Balanced Parentheses https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/
#include <iostream>
#include <string>
#include <vector>
void generateParenthesesRec(std::vector<std::string>& parentheses, const int& n, int open, int close, std::string& current)
{
if (current.length() == 2 * n) {
parentheses.push_back(current);
return;
}
if (open < n) {
current.push_back('(');
generateParenthesesRec(parentheses, n, open + 1, close, current);
current.pop_back();
}
if (close < open) {
current.push_back(')');
generateParenthesesRec(parentheses, n, open, close + 1, current);
current.pop_back();
}
}
std::vector<std::string> generateParentheses(const int& n)
{
std::vector<std::string> parentheses;
std::string current;
generateParenthesesRec(parentheses, n, 0, 0, current);
return parentheses;
}
void print(std::vector<std::string>& parentheses)
{
for (auto p : parentheses) {
std::cout << p << "\n";
}
}
int main()
{
int n = 3;
std::vector<std::string> parentheses = generateParentheses(n);
std::cout << "Balanced parentheses of length " << n << " : \n";
print(parentheses);
return 0;
}