Skip to content

Commit

Permalink
Merge pull request #1841 from Dipanita45/kl
Browse files Browse the repository at this point in the history
Valid Parentheses
  • Loading branch information
pankaj-bind authored Nov 10, 2024
2 parents cfad884 + ef534af commit 7044d42
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
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;
}

0 comments on commit 7044d42

Please sign in to comment.