Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valid Parentheses #1841

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
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;
}
Loading