Skip to content

Commit b70d052

Browse files
authored
Add files via upload
1 parent 0abd857 commit b70d052

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Check redundant brackets
2+
3+
For a given expression in the form of a string, find if there exist any redundant brackets or not. It is given that the expression contains only rounded brackets or parenthesis and the input expression will always be balanced.
4+
A pair of the bracket is said to be redundant when a sub-expression is surrounded by unnecessary or needless brackets.
5+
6+
Example:
7+
Expression: (a+b)+c
8+
Since there are no needless brackets, hence, the output must be 'false'.
9+
10+
Expression: ((a+b))
11+
The expression can be reduced to (a+b). Hence the expression has redundant brackets and the output will be 'true'.
12+
13+
Input Format :
14+
The first and the only line of input contains a string expression, without any spaces in between.
15+
Output Format :
16+
The first and the only line of output will print either 'true' or 'false'(without the quotes) denoting whether the input expression contains redundant brackets or not.
17+
18+
Constraints:
19+
0 <= N <= 10^6
20+
Where N is the length of the expression.
21+
22+
Time Limit: 1 second
23+
Sample Input 1:
24+
a+(b)+c
25+
Sample Output 1:
26+
true
27+
Explanation:
28+
The expression can be reduced to a+b+c. Hence, the brackets are redundant.
29+
Sample Input 2:
30+
(a+b)
31+
Sample Output 2:
32+
false"""
33+
34+
def checkRedundantBrackets(expression) :
35+
s=expression
36+
le = len(s)
37+
l=[]
38+
for i in range(le):
39+
if s[i]=="(":
40+
l.append("(")
41+
elif s[i] in "+-*/":
42+
l.append(s[i])
43+
elif s[i] ==")":
44+
if l[-1]=="(":
45+
return True
46+
elif l[-1] in "+-*/":
47+
while l[-1]!="(":
48+
l.pop()
49+
l.pop()
50+
return False
51+
52+
expression=input("Enter the expression:\n")
53+
print("output:")
54+
if checkRedundantBrackets(expression):
55+
print('true')
56+
else:
57+
print('false')

0 commit comments

Comments
 (0)