Created
December 20, 2012 08:53
-
-
Save Andati/4343919 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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