1
1
/* *
2
2
* @file
3
3
* @brief Generates all combinations of well-formed parentheses.
4
+ * [Generate
5
+ Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/)
4
6
*
5
7
* @details a sequence of parentheses is well-formed if each opening parentheses
6
- has a corresponding closing parenthesis
7
- * and the closing parentheses are correctly ordered
8
+ * has a corresponding closing parenthesis
9
+ * and the closing parentheses are correctly ordered
8
10
*
9
11
* @author [Giuseppe Coco](https://github.com/WoWS17)
10
12
11
13
*/
12
14
13
- #include < cassert>
14
- #include < iostream>
15
- #include < vector>
15
+ #include < cassert> // / for assert
16
+ #include < iostream> // / for I/O operation
17
+ #include < vector> // / for vector container
16
18
19
+ /* *
20
+ * @brief Backtracking algorithms
21
+ * @namespace backtracking
22
+ */
23
+ namespace backtracking {
24
+ /* *
25
+ * @brief generate_parentheses class
26
+ * @namespace generate_parentheses
27
+ */
17
28
class generate_parentheses {
18
29
private:
19
- std::vector<std::string> res; // Contains all possible valid patterns
20
-
21
- /* *
22
- * @brief function that implements backtracking
23
- *
24
- * @param str string build during backtracking
25
- * @param n number of pairs of parentheses
26
- * @param closed number of closed parentheses
27
- * @param open number of open parentheses
28
- */
29
-
30
- void makeStrings (std::string str, int n, int closed, int open) {
31
- if (closed > open) // We can never have more closed than open
32
- return ;
33
-
34
- if (str.length () == 2 * n and
35
- closed != open) // closed and open must be the same
36
- return ;
37
-
38
- if (str.length () == 2 * n) {
39
- res.push_back (str);
40
- return ;
41
- }
42
-
43
- makeStrings (str + ' )' , n, closed + 1 , open);
44
- makeStrings (str + ' (' , n, closed, open + 1 );
45
- }
30
+ std::vector<std::string> res; // /< Contains all possible valid patterns
31
+
32
+ void makeStrings (std::string str, int n, int closed, int open);
46
33
47
34
public:
48
- /* *
49
- * @brief wrapper interface
50
- *
51
- * @param n number of pairs of parentheses
52
- * @return all well-formed pattern of parentheses
53
- */
54
- std::vector<std::string> generate_parenthesis (int n) {
55
- res.clear ();
56
- std::string str = " (" ;
57
- makeStrings (str, n, 0 , 1 );
58
- return res;
59
- }
35
+ std::vector<std::string> generate (int n);
60
36
};
37
+ } // namespace backtracking
38
+
39
+ /* *
40
+ * @brief function that implements backtracking
41
+ *
42
+ * @param str string build during backtracking
43
+ * @param n number of pairs of parentheses
44
+ * @param closed number of closed parentheses
45
+ * @param open number of open parentheses
46
+ */
47
+
48
+ void backtracking::generate_parentheses::makeStrings (std::string str, int n,
49
+ int closed, int open) {
50
+ if (closed > open) // We can never have more closed than open
51
+ return ;
52
+
53
+ if (str.length () == 2 * n and
54
+ closed != open) // closed and open must be the same
55
+ return ;
56
+
57
+ if (str.length () == 2 * n) {
58
+ res.push_back (str);
59
+ return ;
60
+ }
61
+
62
+ makeStrings (str + ' )' , n, closed + 1 , open);
63
+ makeStrings (str + ' (' , n, closed, open + 1 );
64
+ }
65
+
66
+ /* *
67
+ * @brief wrapper interface
68
+ *
69
+ * @param n number of pairs of parentheses
70
+ * @return all well-formed pattern of parentheses
71
+ */
72
+ std::vector<std::string> backtracking::generate_parentheses::generate (int n) {
73
+ backtracking::generate_parentheses::res.clear ();
74
+ std::string str = " (" ;
75
+ backtracking::generate_parentheses::makeStrings (str, n, 0 , 1 );
76
+ return res;
77
+ }
61
78
62
79
/* *
63
80
* @brief Self-test implementations
@@ -66,23 +83,23 @@ class generate_parentheses {
66
83
static void test () {
67
84
int n;
68
85
std::vector<std::string> patterns;
69
- generate_parentheses p;
86
+ backtracking:: generate_parentheses p;
70
87
71
88
n = 1 ;
72
89
patterns = {{" ()" }};
73
- assert (p.generate_parenthesis (n) == patterns);
90
+ assert (p.generate (n) == patterns);
74
91
75
92
n = 3 ;
76
93
patterns = {{" ()()()" }, {" ()(())" }, {" (())()" }, {" (()())" }, {" ((()))" }};
77
94
78
- assert (p.generate_parenthesis (n) == patterns);
95
+ assert (p.generate (n) == patterns);
79
96
80
97
n = 4 ;
81
98
patterns = {{" ()()()()" }, {" ()()(())" }, {" ()(())()" }, {" ()(()())" },
82
99
{" ()((()))" }, {" (())()()" }, {" (())(())" }, {" (()())()" },
83
100
{" (()()())" }, {" (()(()))" }, {" ((()))()" }, {" ((())())" },
84
101
{" ((()()))" }, {" (((())))" }};
85
- assert (p.generate_parenthesis (n) == patterns);
102
+ assert (p.generate (n) == patterns);
86
103
87
104
std::cout << " All tests passed\n " ;
88
105
}
@@ -94,4 +111,4 @@ static void test() {
94
111
int main () {
95
112
test ();
96
113
return 0 ;
97
- }
114
+ }
0 commit comments