3ms C++ Solution using Stack


  • 0
    F
        bool isValid(string s) {
            if(s.length()==0) return true;
            if(s.length()==1 || s.length()%2==1) return false;            
            stack<char> st;       
            st.push(s[0]);
            int i=1;
            while(!st.empty() && i<=s.length()-1){
                char ch=st.top();
                if(!(s[i]==ch+1 || s[i]==ch+2)) 
                   st.push(s[i]);                            
                else st.pop();            
                i++;
            }        
            return st.empty();
        }
    

Log in to reply
 

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