-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1841 from Dipanita45/kl
Valid Parentheses
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
Searching Algorithms/Kth_Largest_Element_Of_The_Array/Valid_Parentheses/Readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
41 changes: 41 additions & 0 deletions
41
Searching Algorithms/Kth_Largest_Element_Of_The_Array/Valid_Parentheses/Valid Parentheses.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
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; | ||
} |