Valid Parentheses c++ solution


  • 0
    Y

    class Solution {
    public:
    bool isValid(string s) {
    unordered_map<char,char> ma={{'(',')'},{'[',']'},{'{','}'}};
    stack<char> st;
    char current_char;
    int tag=0;
    if(s.size()%2==1){
    return false;
    }
    else if(s.size()==0){
    return true;
    }

        st.push(s[0]);
    
        for(int i=1;i<s.size();i++){
            if(ma.find(s[i])!=ma.end()){
                st.push(s[i]);
            }
            else{
                if(ma[st.top()]!=s[i]){
                    return false;
                }
                else{
                    st.pop();
                }
    
            }
        }
        
        if(st.size()!=0)
            return false;
    
    
        return true;
    }
    

    };


Log in to reply
 

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