Skip to content

Instantly share code, notes, and snippets.

@sakalauskas
Created October 27, 2014 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakalauskas/a773906a4de7695acd6c to your computer and use it in GitHub Desktop.
Save sakalauskas/a773906a4de7695acd6c to your computer and use it in GitHub Desktop.
/**
* 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