Skip to content

Commit e6226cf

Browse files
realDuYuanChaogithub-actionscclauss
authored
Balanced parentheses (TheAlgorithms#3768)
* Fixed balanced_parentheses.py * fixed pre-commit * eliminate is_paired * remove unused line * updating DIRECTORY.md * Update data_structures/stacks/balanced_parentheses.py Co-authored-by: Christian Clauss <[email protected]> * Add more test cases * Update data_structures/stacks/balanced_parentheses.py Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent f0c843e commit e6226cf

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@
520520
* [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/other/sierpinski_triangle.py)
521521
* [Tower Of Hanoi](https://github.com/TheAlgorithms/Python/blob/master/other/tower_of_hanoi.py)
522522
* [Triplet Sum](https://github.com/TheAlgorithms/Python/blob/master/other/triplet_sum.py)
523+
* [Two Pointer](https://github.com/TheAlgorithms/Python/blob/master/other/two_pointer.py)
523524
* [Two Sum](https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py)
524525
* [Word Patterns](https://github.com/TheAlgorithms/Python/blob/master/other/word_patterns.py)
525526

Diff for: data_structures/stacks/balanced_parentheses.py

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
from .stack import Stack
22

3-
__author__ = "Omkar Pathak"
43

5-
6-
def balanced_parentheses(parentheses):
7-
""" Use a stack to check if a string of parentheses is balanced."""
8-
stack = Stack(len(parentheses))
9-
for parenthesis in parentheses:
10-
if parenthesis == "(":
11-
stack.push(parenthesis)
12-
elif parenthesis == ")":
13-
if stack.is_empty():
4+
def balanced_parentheses(parentheses: str) -> bool:
5+
"""Use a stack to check if a string of parentheses is balanced.
6+
>>> balanced_parentheses("([]{})")
7+
True
8+
>>> balanced_parentheses("[()]{}{[()()]()}")
9+
True
10+
>>> balanced_parentheses("[(])")
11+
False
12+
>>> balanced_parentheses("1+2*3-4")
13+
True
14+
>>> balanced_parentheses("")
15+
True
16+
"""
17+
stack = Stack()
18+
bracket_pairs = {"(": ")", "[": "]", "{": "}"}
19+
for bracket in parentheses:
20+
if bracket in bracket_pairs:
21+
stack.push(bracket)
22+
elif bracket in (")", "]", "}"):
23+
if stack.is_empty() or bracket_pairs[stack.pop()] != bracket:
1424
return False
15-
stack.pop()
1625
return stack.is_empty()
1726

1827

1928
if __name__ == "__main__":
29+
from doctest import testmod
30+
31+
testmod()
32+
2033
examples = ["((()))", "((())", "(()))"]
2134
print("Balanced parentheses demonstration:\n")
2235
for example in examples:
23-
print(example + ": " + str(balanced_parentheses(example)))
36+
not_str = "" if balanced_parentheses(example) else "not "
37+
print(f"{example} is {not_str}balanced")

0 commit comments

Comments
 (0)