Skip to content

Commit 92d2940

Browse files
Create redundant_braces.cpp
1 parent 82331a0 commit 92d2940

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Given a string A denoting an expression. It contains the following operators ’+’, ‘-‘, ‘*’, ‘/’.
2+
// Check whether A has redundant braces or not.
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
int redundantBraces(string A) {
8+
9+
int n= A.length();
10+
stack <char> s;
11+
12+
for(int i=0; i< n; i++)
13+
{
14+
if(A[i]=='+' || A[i]=='-'|| A[i]=='/'|| A[i]=='*' || A[i]=='(' )
15+
s.push(A[i]);
16+
17+
if(A[i]==')')
18+
{
19+
if(s.top()=='(')
20+
return 1;
21+
22+
int count=0;
23+
while(!s.empty() && s.top()!='(')
24+
{
25+
s.pop();
26+
count++;
27+
}
28+
if(count==0)
29+
return 1;
30+
s.pop();
31+
}
32+
}
33+
34+
return 0;
35+
}
36+
37+
int main()
38+
{
39+
string str;
40+
cin>>str;
41+
if(redundantBraces(str))
42+
cout<<str+" has redundant braces";
43+
else
44+
cout<<str+" doesn't have any redundant braces";
45+
return 0;
46+
47+
}

0 commit comments

Comments
 (0)