Anuj Sunder
Anuj Sunder

Reputation: 47

Runtime Error: Runtime ErrorSegmentation Fault (SIGSEGV) for the code below

I have solved the parenthesis check problem in gfg. when I checked this code with custom input it's working fine and the output is matched . when I submit this code, it shows

Runtime Error: Runtime ErrorSegmentation Fault (SIGSEGV)

bool ispar(string x)
{
    int n=x.length();
    stack <char> s;
    int i=0;
    while(i<n)
    {
        if(x[i]=='['|| x[i]=='('|| x[i]=='{' )
           s.push(x[i]);
         
        
        else if (x[i]==']'&& s.top()=='['|| x[i]==')'&& s.top()=='('||x[i]=='}'&& s.top()=='{')
               s.pop();
        else
             return 0;
        
        
       i++;
    }
       
    
       return (s.empty());

Upvotes: 0

Views: 50

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

You are doing s.top() without checking if s is empty. You should add check for that.

else if (!s.empty() && (x[i]==']'&& s.top()=='['|| x[i]==')'&& s.top()=='('||x[i]=='}'&& s.top()=='{'))
       s.pop();

Upvotes: 3

Related Questions