Untitled

 avatar
user_9124840
plain_text
4 months ago
641 B
1
Indexable
class Solution {
public:
    bool isValid(string s) {

        stack<char> st;

        for (char ch : s) {
            if (ch == '(' || ch == '{' || ch == '[') {
                st.push(ch);
            } else {
                if (st.empty())
                    return false;
                if (ch == ')' && st.top() != '(')
                    return false;
                if (ch == '}' && st.top() != '{')
                    return false;
                if (ch == ']' && st.top() != '[')
                    return false;
                st.pop();
            }
        }

        return st.empty();
    }
};
Leave a Comment