11/* *
22 * @file
33 * @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/)
46 *
57 * @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
810 *
911 * @author [Giuseppe Coco](https://github.com/WoWS17)
1012
1113 */
1214
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
1618
19+ /* *
20+ * @brief Backtracking algorithms
21+ * @namespace backtracking
22+ */
23+ namespace backtracking {
24+ /* *
25+ * @brief generate_parentheses class
26+ * @namespace generate_parentheses
27+ */
1728class generate_parentheses {
1829 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);
4633
4734 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);
6036};
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+ }
6178
6279/* *
6380 * @brief Self-test implementations
@@ -66,23 +83,23 @@ class generate_parentheses {
6683static void test () {
6784 int n;
6885 std::vector<std::string> patterns;
69- generate_parentheses p;
86+ backtracking:: generate_parentheses p;
7087
7188 n = 1 ;
7289 patterns = {{" ()" }};
73- assert (p.generate_parenthesis (n) == patterns);
90+ assert (p.generate (n) == patterns);
7491
7592 n = 3 ;
7693 patterns = {{" ()()()" }, {" ()(())" }, {" (())()" }, {" (()())" }, {" ((()))" }};
7794
78- assert (p.generate_parenthesis (n) == patterns);
95+ assert (p.generate (n) == patterns);
7996
8097 n = 4 ;
8198 patterns = {{" ()()()()" }, {" ()()(())" }, {" ()(())()" }, {" ()(()())" },
8299 {" ()((()))" }, {" (())()()" }, {" (())(())" }, {" (()())()" },
83100 {" (()()())" }, {" (()(()))" }, {" ((()))()" }, {" ((())())" },
84101 {" ((()()))" }, {" (((())))" }};
85- assert (p.generate_parenthesis (n) == patterns);
102+ assert (p.generate (n) == patterns);
86103
87104 std::cout << " All tests passed\n " ;
88105}
@@ -94,4 +111,4 @@ static void test() {
94111int main () {
95112 test ();
96113 return 0 ;
97- }
114+ }
0 commit comments