Stacks: Balanced Brackets

  • + 0 comments

    **Getting seg fault please someone help **

    #include<iostream>
    #include<stack>
    using namespace std;
    bool isBalance(string s,int i=0)
    {
        stack<char>stackk;
        for(int i=0;i<s.length();i++)
        {
                if(s[i]=='[' || s[i]=='{' || s[i]=='(')
                {
                    stackk.push(s[i]);
                }
                else if(s[i]==']' && stackk.top()!='[')
                {
                    return false;
                }
                else if(s[i]==')' && stackk.top()!='(')
                {
                    return false;
                }
                else if(s[i]=='}' && stackk.top()!='{')
                {
                    return false;
                }
                else
                {
                    stackk.pop();
                }
        }
        return true;
    }
    int main()
    {
      int n;
      cin>>n;
      while(n--)
      {
        string s;
        cin>>s;
        if(isBalance(s))
        {
          cout<<"YES"<<endl;
        }
        else
        {
          cout<<"NO"<<endl;
        }
      }
        return 0;
    }