Skip to content

Commit 872353b

Browse files
Update redundant_braces.cpp
1 parent 92d2940 commit 872353b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Data Structures/Stacks/redundant_braces.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,26 @@ int redundantBraces(string A) {
1111

1212
for(int i=0; i< n; i++)
1313
{
14+
//we'll push the operators and opening braces in the stack
1415
if(A[i]=='+' || A[i]=='-'|| A[i]=='/'|| A[i]=='*' || A[i]=='(' )
1516
s.push(A[i]);
16-
17+
18+
//when we encounter closing braces, we need to start popping
1719
if(A[i]==')')
1820
{
21+
//if there are no operators, it means we have redundant braces for eg. ()
1922
if(s.top()=='(')
2023
return 1;
21-
24+
25+
//else we start popping till we encounter opening braces
26+
//count variable will keep count of number of operators enclosed within the braces
2227
int count=0;
2328
while(!s.empty() && s.top()!='(')
2429
{
2530
s.pop();
2631
count++;
2732
}
33+
//count=0 means we have something like (a) which has redundant braces
2834
if(count==0)
2935
return 1;
3036
s.pop();

0 commit comments

Comments
 (0)