Created
April 18, 2015 18:56
-
-
Save stephen-maina/f1c38ecb6e59b2217cd0 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
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