jeudi 11 juillet 2019

Will the OR statement in if check only one condition? Balanced Bracket

I am doing Balanced Bracket using stack. I need to display "Success" if it matches or else output the index(1-indexing) of the first mismatch.

I used stack<pair <char,int>> for storing opening bracket ('{' '(' '[') and the index of the bracket. (I stored index for cases like these "[{}" where the mismatch is opening bracket rather than closing one.

else if(stk.empty()&&(s[i]=='}'||s[i]==')'||s[i]==']'))
{
ans=i+1;
cout<<ans;
break;
}

I am getting Segmentation fault (core dumped) whenever the first character of string is one of the closing brackets( ')' or ']' ). Its working when input is '}' but now when ')' or ']'

Aucun commentaire:

Enregistrer un commentaire