Skip to content

Commit a9a6891

Browse files
Merge pull request #766 from JayanthApagundi/Child
Balanced and Unbalanced Expressions using Stack
2 parents 203036b + 25dc2ce commit a9a6891

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Balanced-Unbalanced-exp.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def isBalanced(expr):
2+
stack = []
3+
for char in expr:
4+
if char in ["(", "{", "["]:
5+
stack.append(char)
6+
else:
7+
if not stack:
8+
return False
9+
current_char = stack.pop()
10+
if current_char == '(':
11+
if char != ")":
12+
return False
13+
if current_char == '{':
14+
if char != "}":
15+
return False
16+
if current_char == '[':
17+
if char != "]":
18+
return False
19+
if stack:
20+
return False
21+
return True
22+
23+
24+
expr = input("Enter the Expression i.e. {},[],()- Combination of these\n")
25+
if isBalanced(expr):
26+
print("Balanced")
27+
else:
28+
print("Not Balanced")

0 commit comments

Comments
 (0)