Skip to content

Commit 6027480

Browse files
giuseppe-cocogithub-actions[bot]Panquesito7realstealthninja
authored
feat: add well-formed parentheses generator (#2445)
* feat: add well-formed parentheses generator * updating DIRECTORY.md * changes made * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal <[email protected]> * chore: apply suggestions from code review * Update backtracking/generate_parentheses.cpp Co-authored-by: realstealthninja <[email protected]> * updating DIRECTORY.md --------- Co-authored-by: github-actions[bot] <[email protected]> Co-authored-by: David Leal <[email protected]> Co-authored-by: realstealthninja <[email protected]>
1 parent 4b740d4 commit 6027480

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
## Backtracking
3+
* [Generate Parentheses](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/generate_parentheses.cpp)
34
* [Graph Coloring](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/graph_coloring.cpp)
45
* [Knight Tour](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/knight_tour.cpp)
56
* [Magic Sequence](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/magic_sequence.cpp)
@@ -67,6 +68,7 @@
6768
* [Queue Using Two Stacks](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/queue_using_two_stacks.cpp)
6869
* [Rb Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/rb_tree.cpp)
6970
* [Reverse A Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/reverse_a_linked_list.cpp)
71+
* [Segment Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/segment_tree.cpp)
7072
* [Skip List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/skip_list.cpp)
7173
* [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/sparse_table.cpp)
7274
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack.hpp)
@@ -228,6 +230,7 @@
228230
* [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/prime_factorization.cpp)
229231
* [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/prime_numbers.cpp)
230232
* [Primes Up To Billion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/primes_up_to_billion.cpp)
233+
* [Quadratic Equations Complex Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/quadratic_equations_complex_numbers.cpp)
231234
* [Realtime Stats](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/realtime_stats.cpp)
232235
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/sieve_of_eratosthenes.cpp)
233236
* [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/sqrt_double.cpp)

backtracking/generate_parentheses.cpp

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

0 commit comments

Comments
 (0)