My Java Solution - Sandy


  • 0
    S

    public boolean isValid(String s){
    int length = s.length();
    if(length%2==1){
    return false;
    }
    char[] c = s.toCharArray();
    char compare;
    Stack<Character> stack = new Stack<>();
    for(int i =0;i<length;i++){
    if((c[i]=='[') || c[i] == '{' || c[i]=='(' ){
    stack.push(c[i]);
    }else{
    if(c[i] == ']'){
    compare ='[';
    }else if(c[i] == '}'){
    compare ='{';
    }else{
    compare ='(';
    }
    if(stack.empty()){
    return false;
    }
    if(compare==stack.peek()){
    stack.pop();
    }else{
    return false;
    }
    }
    }
    if(stack.empty()){
    return true;
    }else{
    return false;
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.