C++ Simple Solution


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

Log in to reply
 

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