TheAlgorithms/C++ 1.0.0
All the 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::string > generate (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::string > res
 Contains all possible valid patterns.
 

Detailed Description

generate_parentheses class

Definition at line 26 of file generate_parentheses.cpp.

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

Definition at line 70 of file generate_parentheses.cpp.

70 {
72 std::string str = "(";
74 return res;
75}
std::vector< std::string > res
Contains all possible valid patterns.
void makeStrings(std::string str, int n, int closed, int open)
function that adds parenthesis to the string.

◆ 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

Definition at line 45 of file generate_parentheses.cpp.

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}

Member Data Documentation

◆ res

std::vector<std::string> backtracking::generate_parentheses::res
private

Contains all possible valid patterns.

Definition at line 28 of file generate_parentheses.cpp.


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