My 0ms C++ Solution


  • 0
    L
    bool isValid(string s) {
    	if(s.size()%2!=0) return 0;
    
    	stack<char> check;
    	int i=0;
    	while(i<s.size()){
    		if(check.empty()) check.push(s[i]);
    		else{
    			if((check.top()=='(' && s[i]==')')||(check.top()=='[' && s[i]==']')||(check.top()=='{' && s[i]=='}'))	check.pop();
    			else check.push(s[i]);
    		}
    		i++;
    	}
    	return check.size()==0;
    }

Log in to reply
 

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