Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add well-formed parentheses generator #2445

Merged
merged 19 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

## Backtracking
* [Generate Parentheses](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/generate_parentheses.cpp)
* [Graph Coloring](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/graph_coloring.cpp)
* [Knight Tour](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/knight_tour.cpp)
* [Magic Sequence](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/magic_sequence.cpp)
Expand Down
113 changes: 113 additions & 0 deletions backtracking/generate_parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @file
* @brief Well-formed [Generated
* Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/) with all combinations.
giuseppe-coco marked this conversation as resolved.
Show resolved Hide resolved
*
* @details 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](https://github.com/WoWS17)

*/

#include <cassert> /// for assert
#include <iostream> /// for I/O operation
#include <vector> /// for vector container

/**
* @brief Backtracking algorithms
* @namespace backtracking
*/
namespace backtracking {
/**
* @brief generate_parentheses class
*/
class generate_parentheses {
Panquesito7 marked this conversation as resolved.
Show resolved Hide resolved
Panquesito7 marked this conversation as resolved.
Show resolved Hide resolved
private:
std::vector<std::string> res; ///< Contains all possible valid patterns

void makeStrings(std::string str, int n, int closed, int open);

public:
std::vector<std::string> generate(int n);
};
Panquesito7 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief function that adds parenthesis to the string.
*
* @param str string build during backtracking
* @param n number of pairs of parentheses
* @param closed number of closed parentheses
* @param open number of open parentheses
*/

void generate_parentheses::makeStrings(std::string str, int n,
int closed, int open) {
if (closed > open) // We can never have more closed than open
return;

if ((str.length() == 2 * n) &&
(closed != open)) { // closed and open must be the same
return;
giuseppe-coco marked this conversation as resolved.
Show resolved Hide resolved
}

if (str.length() == 2 * n) {
res.push_back(str);
return;
}

makeStrings(str + ')', n, closed + 1, open);
makeStrings(str + '(', n, closed, open + 1);
}

/**
* @brief wrapper interface
*
* @param n number of pairs of parentheses
* @return all well-formed pattern of parentheses
*/
std::vector<std::string> generate_parentheses::generate(int n) {
backtracking::generate_parentheses::res.clear();
std::string str = "(";
generate_parentheses::makeStrings(str, n, 0, 1);
return res;
}
giuseppe-coco marked this conversation as resolved.
Show resolved Hide resolved
} // namespace backtracking

/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
int n = 0;
std::vector<std::string> patterns;
backtracking::generate_parentheses p;

n = 1;
patterns = {{"()"}};
assert(p.generate(n) == patterns);

n = 3;
patterns = {{"()()()"}, {"()(())"}, {"(())()"}, {"(()())"}, {"((()))"}};

assert(p.generate(n) == patterns);

n = 4;
patterns = {{"()()()()"}, {"()()(())"}, {"()(())()"}, {"()(()())"},
{"()((()))"}, {"(())()()"}, {"(())(())"}, {"(()())()"},
{"(()()())"}, {"(()(()))"}, {"((()))()"}, {"((())())"},
{"((()()))"}, {"(((())))"}};
assert(p.generate(n) == patterns);

std::cout << "All tests passed\n";
}

/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}