diff --git a/Searching Algorithms/Kth_Largest_Element_Of_The_Array/Valid_Parentheses/Readme.md b/Searching Algorithms/Kth_Largest_Element_Of_The_Array/Valid_Parentheses/Readme.md new file mode 100644 index 00000000..9c7c52dc --- /dev/null +++ b/Searching Algorithms/Kth_Largest_Element_Of_The_Array/Valid_Parentheses/Readme.md @@ -0,0 +1,40 @@ +## Valid Parentheses + +# Problem Description +The problem of "Valid Parentheses" is a common algorithmic challenge where you need to determine if a given string containing just the characters '(', ')', '{', '}', '[' and ']' is valid. A string is considered valid if: + +Open brackets must be closed by the corresponding closing brackets. +Open brackets must be closed in the correct order. + +# Example +Input: s = "()" +Output: true + +Input: s = "()[]{}" +Output: true + +Input: s = "(] " +Output: false + +Input: s = "([)]" +Output: false + +Input: s = "{[]}" +Output: true + +# Solution Approach +A common approach to solving the valid parentheses problem is to use a stack data structure. The stack helps to keep track of the opening brackets and ensures that they are closed in the correct order. + +Steps: +Initialize an empty stack. +Iterate through each character in the string: +If the character is an opening bracket ((, {, [), push it onto the stack. +If the character is a closing bracket (), }, ]): +Check if the stack is empty. If it is, return false (there's no corresponding opening bracket). +Pop the top element from the stack and check if it matches the current closing bracket. If it does not match, return false. +After processing all characters, check if the stack is empty. If it is empty, return true (all opening brackets were matched); otherwise, return false. + +# Time and space complexity +Time Complexity: O(n), where n is the length of the string. We make a single pass through the string, performing constant time operations for each character. + +Space Complexity: O(n) in the worst case, where all characters are opening parentheses, which would require storing them in the stack. In the best case, the space used would be O(1) if the string is valid and balanced. \ No newline at end of file diff --git a/Searching Algorithms/Kth_Largest_Element_Of_The_Array/Valid_Parentheses/Valid Parentheses.c b/Searching Algorithms/Kth_Largest_Element_Of_The_Array/Valid_Parentheses/Valid Parentheses.c new file mode 100644 index 00000000..7587f5a0 --- /dev/null +++ b/Searching Algorithms/Kth_Largest_Element_Of_The_Array/Valid_Parentheses/Valid Parentheses.c @@ -0,0 +1,41 @@ +#include +#include +#include + +bool isValid(char * s) { + int stackSize = 0; + int stackCapacity = 100; // Initial capacity for the stack + char *stack = (char *)malloc(stackCapacity * sizeof(char)); + + for (int i = 0; s[i] != '\0'; i++) { + if (s[i] == '(') { + // Push to stack + if (stackSize == stackCapacity) { + stackCapacity *= 2; // Double the stack size if needed + stack = (char *)realloc(stack, stackCapacity * sizeof(char)); + } + stack[stackSize++] = s[i]; + } else if (s[i] == ')') { + // Pop from stack + if (stackSize == 0) { + free(stack); + return false; // No matching opening parenthesis + } + stackSize--; // Pop the last opening parenthesis + } + } + + bool result = (stackSize == 0); // If stack is empty, valid parentheses + free(stack); + return result; +} + +int main() { + char *s = "(()())"; + if (isValid(s)) { + printf("Valid parentheses\n"); + } else { + printf("Invalid parentheses\n"); + } + return 0; +} \ No newline at end of file