-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
Copy pathgenerate_parentheses.cpp
113 lines (95 loc) · 2.89 KB
/
generate_parentheses.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* @file
* @brief Well-formed [Generated
* Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/) with all combinations.
*
* @details a sequence of parentheses is well-formed if each opening parentheses
* has a corresponding closing parenthesis
* and the closing parentheses are correctly ordered
*
* @author [Giuseppe Coco](https://github.com/WoWS17)
*/
#include <cassert> /// for assert
#include <iostream> /// for I/O operation
#include <vector> /// for vector container
/**
* @brief Backtracking algorithms
* @namespace backtracking
*/
namespace backtracking {
/**
* @brief generate_parentheses class
*/
class generate_parentheses {
private:
std::vector<std::string> res; ///< Contains all possible valid patterns
void makeStrings(std::string str, int n, int closed, int open);
public:
std::vector<std::string> generate(int n);
};
/**
* @brief function that adds parenthesis to the string.
*
* @param str string build during backtracking
* @param n number of pairs of parentheses
* @param closed number of closed parentheses
* @param open number of open parentheses
*/
void generate_parentheses::makeStrings(std::string str, int n,
int closed, int open) {
if (closed > open) // We can never have more closed than open
return;
if ((str.length() == 2 * n) &&
(closed != open)) { // closed and open must be the same
return;
}
if (str.length() == 2 * n) {
res.push_back(str);
return;
}
makeStrings(str + ')', n, closed + 1, open);
makeStrings(str + '(', n, closed, open + 1);
}
/**
* @brief wrapper interface
*
* @param n number of pairs of parentheses
* @return all well-formed pattern of parentheses
*/
std::vector<std::string> generate_parentheses::generate(int n) {
backtracking::generate_parentheses::res.clear();
std::string str = "(";
generate_parentheses::makeStrings(str, n, 0, 1);
return res;
}
} // namespace backtracking
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
int n = 0;
std::vector<std::string> patterns;
backtracking::generate_parentheses p;
n = 1;
patterns = {{"()"}};
assert(p.generate(n) == patterns);
n = 3;
patterns = {{"()()()"}, {"()(())"}, {"(())()"}, {"(()())"}, {"((()))"}};
assert(p.generate(n) == patterns);
n = 4;
patterns = {{"()()()()"}, {"()()(())"}, {"()(())()"}, {"()(()())"},
{"()((()))"}, {"(())()()"}, {"(())(())"}, {"(()())()"},
{"(()()())"}, {"(()(()))"}, {"((()))()"}, {"((())())"},
{"((()()))"}, {"(((())))"}};
assert(p.generate(n) == patterns);
std::cout << "All tests passed\n";
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}