Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created April 18, 2015 19:16
Show Gist options
  • Save stephen-maina/94742ade943342c75eb5 to your computer and use it in GitHub Desktop.
Save stephen-maina/94742ade943342c75eb5 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
Stack<String>stack=new Stack<String>();
for(int index=0;index<S.length();index++){
if(S.charAt(index)=='('){
stack.push("(");
}else{
if(!stack.empty()&&stack.peek().contains("(")){
stack.pop();
}else{
return 0;
}
}
}
if(stack.empty()){
return 1;
}else{
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment