File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ import java .util .Stack ;
3+ public class stackparanthesis {
4+
5+ public static void main (String [] args )
6+ {
7+
8+ Stack <Character > stack =new Stack <Character >();
9+ Scanner s1 =new Scanner (System .in );
10+ System .out .print ("Enter the String containing brackets : " );
11+ String arr =s1 .nextLine ();
12+ if (arr .length ()%2 !=0 )
13+ {
14+ System .out .println ("String is Not Balanced" );
15+
16+ }
17+ else
18+ {
19+ for (int i =0 ;i <arr .length ();i ++)
20+ {
21+ char ch =arr .charAt (i );
22+ if (ch =='{' || ch =='[' || ch =='(' )
23+ {
24+ stack .push (ch );
25+ }
26+ else
27+ {
28+ if (!stack .isEmpty ())
29+ {
30+ char c =stack .peek ();
31+ if ((ch =='}' && c =='{' ) || (ch ==']' && c =='[' ) ||(ch ==')' && c =='(' ))
32+ {
33+ stack .pop ();
34+ }
35+ }
36+ }
37+ }
38+ if (stack .isEmpty ())
39+ {
40+ System .out .println ("String is Balanced" );
41+ }
42+ else
43+ {
44+ System .out .println ("String is Not Balanced" );
45+ }
46+
47+ }
48+ }
49+
50+ }
You can’t perform that action at this time.
0 commit comments