Skip to content

Instantly share code, notes, and snippets.

@Andati
Created December 20, 2012 08:53
Show Gist options
  • Save Andati/4343919 to your computer and use it in GitHub Desktop.
Save Andati/4343919 to your computer and use it in GitHub Desktop.
/*
* Given a string of brackets, devise an algorithm that checks whether they are balanced
*/
import java.util.Stack;
class Parenthesis {
public static void main(String[] args){
Stack<Character> s = new Stack<Character>();
String str = "(((())))))";
for(int i=0; i<str.length(); i++){
if(str.charAt(i)=='(') s.push('(');
else {
if(s.empty()) {
System.out.println("Not Balanced");
return;
}
s.pop();
}
}
if(s.empty()) System.out.println("Balanced");
else System.out.println("Not Balanced");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment