Skip to content

Commit 977dd87

Browse files
authored
Merge pull request #154 from SumitMangrati/main
added java program
2 parents 46787be + 3ec2719 commit 977dd87

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Coding/Balanced_Brackets.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

0 commit comments

Comments
 (0)