File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -11,20 +11,26 @@ int redundantBraces(string A) {
11
11
12
12
for (int i=0 ; i< n; i++)
13
13
{
14
+ // we'll push the operators and opening braces in the stack
14
15
if (A[i]==' +' || A[i]==' -' || A[i]==' /' || A[i]==' *' || A[i]==' (' )
15
16
s.push (A[i]);
16
-
17
+
18
+ // when we encounter closing braces, we need to start popping
17
19
if (A[i]==' )' )
18
20
{
21
+ // if there are no operators, it means we have redundant braces for eg. ()
19
22
if (s.top ()==' (' )
20
23
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
22
27
int count=0 ;
23
28
while (!s.empty () && s.top ()!=' (' )
24
29
{
25
30
s.pop ();
26
31
count++;
27
32
}
33
+ // count=0 means we have something like (a) which has redundant braces
28
34
if (count==0 )
29
35
return 1 ;
30
36
s.pop ();
You can’t perform that action at this time.
0 commit comments