feat : add LeetCode problem 22

This commit is contained in:
Parag Jain 2023-09-30 17:23:54 +05:30
parent e5dad3fa8d
commit b82d01428b
2 changed files with 52 additions and 1 deletions

View File

@ -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 |

50
leetcode/src/22.c Normal file
View File

@ -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;
}