I am getting run time error for "()", how ever my code pass"()" and any possible input i give it


  • 0
    B

    class Solution {
    public:
    bool isValid(string s)
    {

    int word_len=s.length();

    bool valid=0;
    int counter;
    if (word_len==0)    return 1;
    if (word_len%2 !=0) return 0;
    for (int i=0;i<word_len;i++)
    {
    	int save_counter=counter;
    
    	if(s[i]=='}' || s[i]==']' || s[i]==')')
    		{
    			for (int k=0;k<save_counter;k++)
    			{
    
    				if( ( s[i-1-k]=='[' && s[i+k]==']' ) || (  s[i-1-k]=='(' && s[i+k]==')' )  || ( s[i-1-k]=='{' && s[i+k]=='}' ) )
    			        valid=1;
    			    else	return 0;
    				
    			}
    
    			i += counter;
    			counter=0;
    		}
    	else counter++;
    }
        return valid;
    }
    

    };


Log in to reply
 

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