Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 697 Bytes

46.md

File metadata and controls

25 lines (21 loc) · 697 Bytes

Valid Parenthesis

LeetCode Link

    bool isValid(string s) {
       stack<char> ans;
        for(int i = 0; i < s.size(); i++) {
            if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
                ans.push(s[i]);
            } else {
                if(ans.empty()){
                    return false;
                }
                char top = ans.top();
                ans.pop();

                
                if ((s[i] == ')' && top != '(') || (s[i] == '}' && top != '{') || (s[i] == ']' && top != '[')) {
                    return false;
                }

            }
        }
        return ans.empty();
    }