My Simple Code


  • 0
    8

    class Solution {
    public:
    bool isValid(string s) {

        stack<char> mystack;
        for(int i=0;i<s.size();i++)
        {
            char c=s[i];
            if(c=='(' || c=='{' || c=='[')
            {
               mystack.push(c);
            }
            else
            {
                if(!mystack.empty())
                {
                    char cprev = mystack.top();
                    if(c==')' && cprev!='(')
                      return false;
                    if(c=='}' && cprev!='{')
                      return false;
                    if(c==']' && cprev!='[')
                      return false;
                    mystack.pop();
                }
                else
                   return false;
            }
        }
        if(mystack.empty())
        return true;
        else
        return false;
    }
    

    };


Log in to reply
 

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