• + 26 comments
    string isBalanced(string s) {
        // Complete this function
        std::stack<char> st;
        st.push('@');
        
        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='('||s[i]=='['||s[i]=='{')
                st.push(s[i]);
            else
            {
                while(!st.empty()&&((s[i]==')'&&st.top()=='(')||(s[i]==']'&&st.top()=='[')||(s[i]=='}'&&st.top()=='{')))
                      {
                          st.pop();
                      }
            }
        }
                      if(st.top()=='@')
                      return "YES";
                      else
                      return "NO";
    }
    

    why this code fail for two of the testcases.