Skip to content

Commit e136075

Browse files
committed
feat: validate-parentheses challenge
1 parent 1a4c116 commit e136075

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed
Binary file not shown.

1-EASY/Python/11-merge-overlapping-intervals/proposed-solution/intervals.py renamed to 2-NORMAL/Python/11-merge-overlapping-intervals/proposed-solution/intervals.py

File renamed without changes.

1-EASY/Python/11-merge-overlapping-intervals/proposed-solution/main.py renamed to 2-NORMAL/Python/11-merge-overlapping-intervals/proposed-solution/main.py

File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Challenge: Create a function that, given a string composed of parentheses and possibly other opening and closing symbols, verifies if the sequence is valid (i.e., each open symbol has its corresponding closing symbol in the correct order). Use a stack data structure to solve it.
2+
3+
from parentheses import is_valid_parentheses
4+
5+
def main():
6+
test_strings = ["()", "()[]{}", "(]", "([)]", "{[]}"]
7+
for s in test_strings:
8+
print(f"Is '{s}' valid? {is_valid_parentheses(s)}")
9+
10+
if __name__ == "__main__":
11+
main()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def is_valid_parentheses(s):
2+
stack = []
3+
mapping = {')': '(', ']': '[', '}': '{'}
4+
5+
for char in s:
6+
if char in mapping.values():
7+
stack.append(char)
8+
elif char in mapping:
9+
if not stack or stack.pop() != mapping[char]:
10+
return False
11+
else:
12+
# Ignore other characters
13+
continue
14+
15+
return not stack

0 commit comments

Comments
 (0)