From d74a6a59bf21dd4fc4d460272f56f47f674e9a8a Mon Sep 17 00:00:00 2001 From: WoWL17 Date: Wed, 12 Apr 2023 09:35:32 +0200 Subject: [PATCH] changes made --- backtracking/generate_parentheses.cpp | 115 +++++++++++++++----------- 1 file changed, 66 insertions(+), 49 deletions(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index 6c22133331d..b3049b752f6 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -1,63 +1,80 @@ /** * @file * @brief Generates all combinations of well-formed parentheses. + * [Generate + Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/) * * @details a sequence of parentheses is well-formed if each opening parentheses - has a corresponding closing parenthesis - * and the closing parentheses are correctly ordered + * has a corresponding closing parenthesis + * and the closing parentheses are correctly ordered * * @author [Giuseppe Coco](https://github.com/WoWS17) */ -#include -#include -#include +#include /// for assert +#include /// for I/O operation +#include /// for vector container +/** + * @brief Backtracking algorithms + * @namespace backtracking + */ +namespace backtracking { +/** + * @brief generate_parentheses class + * @namespace generate_parentheses + */ class generate_parentheses { private: - std::vector res; // Contains all possible valid patterns - - /** - * @brief function that implements backtracking - * - * @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 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 and - closed != open) // closed and open must be the same - return; - - if (str.length() == 2 * n) { - res.push_back(str); - return; - } - - makeStrings(str + ')', n, closed + 1, open); - makeStrings(str + '(', n, closed, open + 1); - } + std::vector res; ///< Contains all possible valid patterns + + void makeStrings(std::string str, int n, int closed, int open); public: - /** - * @brief wrapper interface - * - * @param n number of pairs of parentheses - * @return all well-formed pattern of parentheses - */ - std::vector generate_parenthesis(int n) { - res.clear(); - std::string str = "("; - makeStrings(str, n, 0, 1); - return res; - } + std::vector generate(int n); }; +} // namespace backtracking + +/** + * @brief function that implements backtracking + * + * @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 backtracking::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 and + closed != open) // closed and open must be the same + return; + + 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 backtracking::generate_parentheses::generate(int n) { + backtracking::generate_parentheses::res.clear(); + std::string str = "("; + backtracking::generate_parentheses::makeStrings(str, n, 0, 1); + return res; +} /** * @brief Self-test implementations @@ -66,23 +83,23 @@ class generate_parentheses { static void test() { int n; std::vector patterns; - generate_parentheses p; + backtracking::generate_parentheses p; n = 1; patterns = {{"()"}}; - assert(p.generate_parenthesis(n) == patterns); + assert(p.generate(n) == patterns); n = 3; patterns = {{"()()()"}, {"()(())"}, {"(())()"}, {"(()())"}, {"((()))"}}; - assert(p.generate_parenthesis(n) == patterns); + assert(p.generate(n) == patterns); n = 4; patterns = {{"()()()()"}, {"()()(())"}, {"()(())()"}, {"()(()())"}, {"()((()))"}, {"(())()()"}, {"(())(())"}, {"(()())()"}, {"(()()())"}, {"(()(()))"}, {"((()))()"}, {"((())())"}, {"((()()))"}, {"(((())))"}}; - assert(p.generate_parenthesis(n) == patterns); + assert(p.generate(n) == patterns); std::cout << "All tests passed\n"; } @@ -94,4 +111,4 @@ static void test() { int main() { test(); return 0; -} \ No newline at end of file +}