Skip to content

Commit

Permalink
Create Balanced_Brackets.java
Browse files Browse the repository at this point in the history
  • Loading branch information
shivani9-codes authored Oct 8, 2020
1 parent 49e3649 commit 3eebb59
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Balanced_Brackets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();

Stack<Character> st = new Stack<>();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == '(' || ch == '{' || ch == '[') {
st.push(ch);
} else if (ch == ')') {
if (st.size() == 0 || st.peek() != '(') {
System.out.println(false);
return;
} else {
st.pop();
}
} else if (ch == '}') {
if (st.size() == 0 || st.peek() != '{') {
System.out.println(false);
return;
} else {
st.pop();
}
} else if (ch == ']') {
if (st.size() == 0 || st.peek() != '[') {
System.out.println(false);
return;
} else {
st.pop();
}
} else {
// nothing
}
}

if (st.size() == 0) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}

0 comments on commit 3eebb59

Please sign in to comment.