Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created April 18, 2015 18:56
Show Gist options
  • Save stephen-maina/f1c38ecb6e59b2217cd0 to your computer and use it in GitHub Desktop.
Save stephen-maina/f1c38ecb6e59b2217cd0 to your computer and use it in GitHub Desktop.
import java.util.Stack;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
char [] test= S.toCharArray();
Stack<String> container=new Stack<String>();
for (int index=0;index<test.length;index++){
char popped=test[index];
if(container.empty()){
container.push(String.valueOf(popped));
continue;
}
if(popped=='{'){
container.push("{");
}else if(popped=='}'){
if(container.peek().contains("{")){
container.pop();
}else{
return 0;
}
}else if(popped=='['){
container.push("[");
}else if(popped==']'){
if(container.peek().contains("[")){
container.pop();
}else{
return 0;
}
}else if(popped=='('){
container.push("(");
}else if(popped==')'){
if(container.peek().contains("(")){
container.pop();
}else{
return 0;
}
}
}
if(container.empty()){
return 1;
}else{
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment