From a83d0cf8e998651da31d5dbb7edc96d660084bd7 Mon Sep 17 00:00:00 2001 From: WoWL17 Date: Fri, 7 Apr 2023 15:44:40 +0200 Subject: [PATCH 01/17] feat: add well-formed parentheses generator --- backtracking/generate_parentheses.cpp | 97 +++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 backtracking/generate_parentheses.cpp diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp new file mode 100644 index 00000000000..6c22133331d --- /dev/null +++ b/backtracking/generate_parentheses.cpp @@ -0,0 +1,97 @@ +/** + * @file + * @brief Generates all combinations of well-formed parentheses. + * + * @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 +#include +#include + +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); + } + + 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; + } +}; + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + int n; + std::vector patterns; + generate_parentheses p; + + n = 1; + patterns = {{"()"}}; + assert(p.generate_parenthesis(n) == patterns); + + n = 3; + patterns = {{"()()()"}, {"()(())"}, {"(())()"}, {"(()())"}, {"((()))"}}; + + assert(p.generate_parenthesis(n) == patterns); + + n = 4; + patterns = {{"()()()()"}, {"()()(())"}, {"()(())()"}, {"()(()())"}, + {"()((()))"}, {"(())()()"}, {"(())(())"}, {"(()())()"}, + {"(()()())"}, {"(()(()))"}, {"((()))()"}, {"((())())"}, + {"((()()))"}, {"(((())))"}}; + assert(p.generate_parenthesis(n) == patterns); + + std::cout << "All tests passed\n"; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); + return 0; +} \ No newline at end of file From ffeb9157d997d6f18b8c9de83573b4d445249667 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 7 Apr 2023 13:50:36 +0000 Subject: [PATCH 02/17] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 99c1c71b3ed..33fc90dd956 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) From d74a6a59bf21dd4fc4d460272f56f47f674e9a8a Mon Sep 17 00:00:00 2001 From: WoWL17 Date: Wed, 12 Apr 2023 09:35:32 +0200 Subject: [PATCH 03/17] 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 +} From e9bc5dc3bb488c0fd520dcc68839822f03d32de5 Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Sat, 15 Apr 2023 10:58:20 +0200 Subject: [PATCH 04/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index b3049b752f6..9ca77bc5011 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -1,8 +1,7 @@ /** * @file - * @brief Generates all combinations of well-formed parentheses. - * [Generate - Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/) + * @brief Well-formed [Generated + Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/) with all combinations. * * @details a sequence of parentheses is well-formed if each opening parentheses * has a corresponding closing parenthesis From 46c4cc19e44212be0ad652acd01bc0846dcbd94b Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Sat, 15 Apr 2023 10:59:51 +0200 Subject: [PATCH 05/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index 9ca77bc5011..3910e12f0f1 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -22,7 +22,6 @@ namespace backtracking { /** * @brief generate_parentheses class - * @namespace generate_parentheses */ class generate_parentheses { private: From efca21d8b0b87d77cbaf9c41a226c86c4d8d548d Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Wed, 26 Apr 2023 18:36:04 +0200 Subject: [PATCH 06/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index 3910e12f0f1..daa605b7376 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -48,8 +48,8 @@ void backtracking::generate_parentheses::makeStrings(std::string str, int n, 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 + if ((str.length() == 2 * n) && + (closed != open)) { // closed and open must be the same return; if (str.length() == 2 * n) { From 9109ced5317a4f8b964bbe71355d9c17cd5f9e28 Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Wed, 26 Apr 2023 18:36:31 +0200 Subject: [PATCH 07/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index daa605b7376..cd032a06457 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -51,6 +51,7 @@ void backtracking::generate_parentheses::makeStrings(std::string str, int n, if ((str.length() == 2 * n) && (closed != open)) { // closed and open must be the same return; + } if (str.length() == 2 * n) { res.push_back(str); From 6f790f33cee6d048419fbca4673369f5e770942d Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:35:21 +0200 Subject: [PATCH 08/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index cd032a06457..1460168a89b 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -1,7 +1,7 @@ /** * @file * @brief Well-formed [Generated - Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/) with all combinations. + * Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/) with all combinations. * * @details a sequence of parentheses is well-formed if each opening parentheses * has a corresponding closing parenthesis From a8209d2bb5e75a6cb5e0ef775ce9e7e8133a0072 Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:35:36 +0200 Subject: [PATCH 09/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index 1460168a89b..485d085adce 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -74,6 +74,7 @@ std::vector backtracking::generate_parentheses::generate(int n) { backtracking::generate_parentheses::makeStrings(str, n, 0, 1); return res; } +} // namespace backtracking /** * @brief Self-test implementations From 5029e71c59959d101cbbacb4215c329cb0b9a569 Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:35:49 +0200 Subject: [PATCH 10/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index 485d085adce..c68bcd1840a 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -71,7 +71,7 @@ void backtracking::generate_parentheses::makeStrings(std::string str, int n, std::vector backtracking::generate_parentheses::generate(int n) { backtracking::generate_parentheses::res.clear(); std::string str = "("; - backtracking::generate_parentheses::makeStrings(str, n, 0, 1); + generate_parentheses::makeStrings(str, n, 0, 1); return res; } } // namespace backtracking From e1fafd05ab216ca73e2d451bc8990222c303fe39 Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:36:05 +0200 Subject: [PATCH 11/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index c68bcd1840a..ee940cf2df8 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -68,7 +68,7 @@ void backtracking::generate_parentheses::makeStrings(std::string str, int n, * @param n number of pairs of parentheses * @return all well-formed pattern of parentheses */ -std::vector backtracking::generate_parentheses::generate(int n) { +std::vector generate_parentheses::generate(int n) { backtracking::generate_parentheses::res.clear(); std::string str = "("; generate_parentheses::makeStrings(str, n, 0, 1); From 3264a4104ef0a928c5fd26564fb0b279554473d1 Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:36:19 +0200 Subject: [PATCH 12/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index ee940cf2df8..c751681f886 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -43,7 +43,7 @@ class generate_parentheses { * @param open number of open parentheses */ -void backtracking::generate_parentheses::makeStrings(std::string str, int n, +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; From 352cf4dfc61cb33f7afb2208a9f955aafd57f20d Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:36:35 +0200 Subject: [PATCH 13/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index c751681f886..bf7cb234dca 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -81,7 +81,7 @@ std::vector generate_parentheses::generate(int n) { * @returns void */ static void test() { - int n; + int n = 0; std::vector patterns; backtracking::generate_parentheses p; From 18a085167bbbf6f5e7ba9f73fb13112fa8ddb6de Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:37:18 +0200 Subject: [PATCH 14/17] Update backtracking/generate_parentheses.cpp Co-authored-by: David Leal --- backtracking/generate_parentheses.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index bf7cb234dca..790ae920c96 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -32,7 +32,6 @@ class generate_parentheses { public: std::vector generate(int n); }; -} // namespace backtracking /** * @brief function that implements backtracking From 3b247a6f4017442737b0b9bf9d9382dda00a1cbd Mon Sep 17 00:00:00 2001 From: David Leal Date: Wed, 26 Apr 2023 12:30:48 -0600 Subject: [PATCH 15/17] chore: apply suggestions from code review --- backtracking/generate_parentheses.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index 790ae920c96..df94f7c9c60 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -108,6 +108,6 @@ static void test() { * @returns 0 on exit */ int main() { - test(); + test(); // run self-test implementations return 0; } From 728dddefe2d39d9497a3ece694aed44798643509 Mon Sep 17 00:00:00 2001 From: WoWS17 <76009241+WoWS17@users.noreply.github.com> Date: Thu, 27 Apr 2023 09:42:17 +0200 Subject: [PATCH 16/17] Update backtracking/generate_parentheses.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- backtracking/generate_parentheses.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/generate_parentheses.cpp b/backtracking/generate_parentheses.cpp index df94f7c9c60..8a9e3b3301d 100644 --- a/backtracking/generate_parentheses.cpp +++ b/backtracking/generate_parentheses.cpp @@ -34,7 +34,7 @@ class generate_parentheses { }; /** - * @brief function that implements backtracking + * @brief function that adds parenthesis to the string. * * @param str string build during backtracking * @param n number of pairs of parentheses From 5fada6330dbb09e62734c9698e39ee3091e545ee Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 2 May 2023 06:52:13 +0000 Subject: [PATCH 17/17] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6f077e8f447..817fdd89bef 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -68,6 +68,7 @@ * [Queue Using Two Stacks](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/queue_using_two_stacks.cpp) * [Rb Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/rb_tree.cpp) * [Reverse A Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/reverse_a_linked_list.cpp) + * [Segment Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/segment_tree.cpp) * [Skip List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/skip_list.cpp) * [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/sparse_table.cpp) * [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack.hpp) @@ -229,6 +230,7 @@ * [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/prime_factorization.cpp) * [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/prime_numbers.cpp) * [Primes Up To Billion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/primes_up_to_billion.cpp) + * [Quadratic Equations Complex Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/quadratic_equations_complex_numbers.cpp) * [Realtime Stats](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/realtime_stats.cpp) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/sieve_of_eratosthenes.cpp) * [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/sqrt_double.cpp)