TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
generate_parentheses.cpp File Reference

Well-formed Generated Parentheses with all combinations. More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for generate_parentheses.cpp:

Go to the source code of this file.

Classes

class  backtracking::generate_parentheses
 generate_parentheses class More...
 

Namespaces

namespace  backtracking
 for vector container
 

Functions

static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Well-formed Generated Parentheses with all combinations.

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

Definition in file generate_parentheses.cpp.

Function Documentation

◆ main()

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
Examples
/Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/iterative_factorial.cpp, /Users/runner/work/C-Plus-Plus/C-Plus-Plus/math/quadratic_equations_complex_numbers.cpp, /Users/runner/work/C-Plus-Plus/C-Plus-Plus/numerical_methods/rungekutta.cpp, and /Users/runner/work/C-Plus-Plus/C-Plus-Plus/sorting/wiggle_sort.cpp.

Definition at line 82 of file generate_parentheses.cpp.

82 {
83 int n = 0;
84 std::vector<std::string> patterns;
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}
std::vector< std::string > generate(int n)
wrapper interface