Reputation: 47
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
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