java most strightforward solution


  • 0
    Y

    class Solution {
    public boolean isValid(String s) {
    Stack<Character> st = new Stack<Character>();
    for(char c : s.toCharArray()) {
    if(c == '(' || c == '[' || c == '{'){
    st.push(c);
    }
    else
    {
    if (st.isEmpty() == true) {return false;}
    if (( c==')' && st.peek()!='(')|| ( c=='}' && st.peek()!='{')||( c==']' && st.peek()!='['))
    {return false;}
    st.pop();
    }
    }
    return st.empty();
    }
    }


Log in to reply
 

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