Skip to content

Latest commit

 

History

History
44 lines (36 loc) · 1.06 KB

valid-parenthesis-string.md

File metadata and controls

44 lines (36 loc) · 1.06 KB

Solution

    class Solution {
    public:
        bool checkValidString(string s) {
            stack<int> s1;
            stack<int> s2;
            
            for(int i = 0; i < s.length(); i++) {
                if(s[i] == '(')
                    s1.push(i);
                else if(s[i] == '*')
                    s2.push(i);
                else {
                    if(!s1.empty())
                        s1.pop();
                    else if(!s2.empty())
                        s2.pop();
                    else 
                        return false;
                }
            }
            while(!s1.empty() && !s2.empty()) {
                if(s1.top() > s2.top()) 
                    return false;
                s1.pop();
                s2.pop();
            }
            if(!s1.empty()) 
                return false;
            return true;
            
        }
    };