20. Valid Parentheses

C++ | Easy Understanding | Brute Force | Easy | 100% Success 

class Solution {

public:

    bool isValid(string s) {

        stack<char> st;  

        for(auto i:s)  

        {

            if(i=='(' or i=='{' or i=='[') 

                st.push(i); 

            else   

            {

                if(st.empty() or (st.top()=='(' and i!=')') or (st.top()=='{' and i!='}') or                        (st.top()=='[' and i!=']')) 

                return false;

                st.pop();  

            }

        }

        return st.empty();  

    }

};

Comments

Popular posts from this blog

1431. Kids With the Greatest Number of Candies

125. Valid Palindrome

771. Jewels and Stones