Created
October 27, 2014 10:38
-
-
Save sakalauskas/a773906a4de7695acd6c to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Created by cmb13162 on 20/10/14. | |
*/ | |
public class ParenthesisMatcher { | |
protected StackADT<Character> stack; | |
// a constructor | |
public ParenthesisMatcher(StackADT<Character> stackInterface) { | |
this.stack = stackInterface; | |
} | |
public boolean isParenthesisMatch(String str) { | |
for (int i = 0; i < str.length(); i++) | |
{ | |
char current = str.charAt(i); | |
switch (current) { | |
case '{': | |
case '(': | |
case '[': | |
this.stack.push(current); | |
break; | |
case '}': | |
case ')': | |
case ']': | |
if (stack.isEmpty()) { | |
return false; | |
} | |
char last = stack.top(); | |
if ((current == '}' && last == '{') || (current == ')' && last == '(') || (current == ']' && last == '[') ) { | |
stack.pop(); | |
} | |
else { | |
return false; | |
} | |
break; | |
} | |
} | |
return stack.isEmpty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment