Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
backtracking::generate_parentheses Class Reference

generate_parentheses class More...

Collaboration diagram for backtracking::generate_parentheses:
[legend]

Public Member Functions

std::vector< std::stringgenerate (int n)
 wrapper interface
 

Private Member Functions

void makeStrings (std::string str, int n, int closed, int open)
 function that adds parenthesis to the string.
 

Private Attributes

std::vector< std::stringres
 Contains all possible valid patterns.
 

Detailed Description

Member Function Documentation

◆ generate()

std::vector< std::string > backtracking::generate_parentheses::generate ( int n)

wrapper interface

Parameters
nnumber of pairs of parentheses
Returns
all well-formed pattern of parentheses
70 {
72 std::string str = "(";
74 return res;
75}
std::vector< std::string > res
Contains all possible valid patterns.
Definition generate_parentheses.cpp:28
void makeStrings(std::string str, int n, int closed, int open)
function that adds parenthesis to the string.
Definition generate_parentheses.cpp:45
T clear(T... args)
Here is the call graph for this function:

◆ makeStrings()

void backtracking::generate_parentheses::makeStrings ( std::string str,
int n,
int closed,
int open )
private

function that adds parenthesis to the string.

Parameters
strstring build during backtracking
nnumber of pairs of parentheses
closednumber of closed parentheses
opennumber of open parentheses
46 {
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}
T push_back(T... args)
T length(T... args)
Here is the call graph for this function:

The documentation for this class was generated from the following file: