11 lines c++ concise and easy understand solution


  • 1
    A
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> st;
            for(auto c : s){
                if(c == '(' || c == '{' || c == '[') st.push(c);
                else if(st.size() > 0){
                    if(c == ')' && st.top() == '(') st.pop();
                    else if (c == '}' && st.top() == '{') st.pop();
                    else if (c == ']' && st.top() == '[') st.pop();
                    else return false;
                } else return false;
            }
            return st.size() == 0;
        }
    };

Log in to reply
 

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