File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ import java .util .Stack ;
3+
4+ public class Balanced_Brackets {
5+ public static boolean solution (String s ){
6+ Stack <Character > st =new Stack <>();
7+ for (int i =0 ;i <s .length ();i ++){
8+ char ch =s .charAt (i );
9+ if (ch ==')' ){
10+ if (st .size ()==0 ){
11+ return false ;
12+ }
13+ while (st .peek ()!='(' ){
14+ st .pop ();
15+ }
16+ st .pop ();
17+ }
18+ else if (ch =='}' ){
19+ if (st .size ()==0 ){
20+ return false ;
21+ }
22+ while (st .peek ()!='{' ){
23+ st .pop ();
24+ }
25+ st .pop ();
26+ }
27+ else if (ch ==']' ){
28+ if (st .size ()==0 ){
29+ return false ;
30+ }
31+ while (st .peek ()!='[' ){
32+ st .pop ();
33+ }
34+ st .pop ();
35+ }
36+ else {
37+ st .push (ch );
38+ }
39+ }
40+ if (st .size ()==0 ){
41+ return true ;
42+ }
43+ return false ;
44+ }
45+ public static void main (String [] args ) {
46+ String s1 ="[(a+b)+{(c+d)+(e/f)}]" ;
47+ String s2 ="[(a+b)+{(c+d)+(e/f)]}" ;
48+ String s3 ="[(a+b)+{(c+d)+(e/f)}" ;
49+ System .out .println (solution (s3 ));
50+ }
51+ }
You can’t perform that action at this time.
0 commit comments