From b82d01428bae5dd0b26c4522ff26c56c79881866 Mon Sep 17 00:00:00 2001 From: Parag Jain Date: Sat, 30 Sep 2023 17:23:54 +0530 Subject: [PATCH] feat : add LeetCode problem 22 --- leetcode/DIRECTORY.md | 3 ++- leetcode/src/22.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 leetcode/src/22.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index d4436ea4..bbb9820d 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -23,7 +23,8 @@ | 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number) | [C](./src/17.c) | Medium | | 19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list) | [C](./src/19.c) | Medium | | 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses) | [C](./src/20.c) | Easy | -| 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists) | [C](./src/21.c) | Easy | +| 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists) | [C](./src/21.c) | Easy +| 22 | [Generate-parentheses](https://leetcode.com/problems/generate-parentheses) | [C](./src/22.c) | Medium | | 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs) | [C](./src/24.c) | Medium | | 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array) | [C](./src/26.c) | Easy | | 27 | [Remove Element](https://leetcode.com/problems/remove-element) | [C](./src/27.c) | Easy | diff --git a/leetcode/src/22.c b/leetcode/src/22.c new file mode 100644 index 00000000..6e9bbf09 --- /dev/null +++ b/leetcode/src/22.c @@ -0,0 +1,50 @@ +#define MAX_COMBINATIONS 1431 + +int combinationIndex; + +// Recursive function to generate valid parentheses combinations +void generateParenthesesUtil(int totalParentheses, int openCount, int closeCount, char **combinations, char *currentCombination, int currentIndex) { + // Base case: If the total count of open and close parentheses equals n, add the current combination to the result array + if ((openCount + closeCount) == totalParentheses) { + combinations[combinationIndex] = currentCombination; + combinationIndex++; + return; + } + + // If the number of open and close parentheses is equal, append a new open parenthesis + if (openCount == closeCount) { + currentCombination[currentIndex] = '('; + generateParenthesesUtil(totalParentheses, openCount + 1, closeCount, combinations, currentCombination, currentIndex + 1); + return; + } + + // If there are more open parentheses than close parentheses, try adding a close parenthesis + if (openCount > closeCount) { + currentCombination[currentIndex] = ')'; + generateParenthesesUtil(totalParentheses, openCount, closeCount + 1, combinations, currentCombination, currentIndex + 1); + + // If the number of open parentheses is less than half of n, try adding an open parenthesis + if (openCount < totalParentheses / 2) { + char *newCombination = (char *)malloc(sizeof(char) * (totalParentheses + 1)); + memcpy(newCombination, currentCombination, currentIndex); + newCombination[totalParentheses] = '\0'; + newCombination[currentIndex] = '('; + generateParenthesesUtil(totalParentheses, openCount + 1, closeCount, combinations, newCombination, currentIndex + 1); + } + } +} + +// Function to generate all valid combinations of parentheses +char **generateParenthesis(int n, int *numCombinations) { + combinationIndex = 0; + char **combinations = (char **)malloc(MAX_COMBINATIONS * sizeof(char *)); + char *currentCombination = (char *)malloc(((n * 2) + 1) * sizeof(char)); + currentCombination[n * 2] = '\0'; + + // Start the recursive generation process + generateParenthesesUtil(n * 2, 0, 0, combinations, currentCombination, 0); + + // Set the number of combinations and return the generated array + *numCombinations = combinationIndex; + return combinations; +}