423.Valid Parentheses
1.Description(Medium)
2.Code
public boolean isValidParentheses(String s) {
if(s==null || s.length()==0){
return true;
}
char[] str=s.toCharArray();
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<str.length;i++){
char current=str[i];
if(current=='(' || current=='{' || current=='['){
stack.push(current);
}
if(current==')'){
if(stack.isEmpty() || stack.peek()!='(' ){
return false;
}else{
stack.pop();
}
}
if(current=='}'){
if(stack.isEmpty() || stack.peek()!='{' ){
return false;
}else{
stack.pop();
}
}
if(current==']'){
if(stack.isEmpty() || stack.peek()!='[' ){
return false;
}else{
stack.pop();
}
}
}
if(stack.isEmpty()){
return true;
}else{
return false;
}
}Last updated