Compiler and parsers question Third test case failing giving 0

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main() {
ll t;
cin>>t ;
while(t–)
{
int count=0;
string s;
cin>> s;

    std::stack<char> str;
    
    for(int i=0;i<s.length();i++)
    {
        if(s[i]=='<')
        str.push(s[i]);
        
        else
        {
            if(str.empty())
            {
                count=0;
                break;
            }
            else
            {
                str.pop();
                count++;
            }
        }
        
    }
    
    if(!str.empty())
        count=0;
      
    
    
    cout<<(count*2)<<endl;
    
 
}


return 0;

}

Format your code it’s messed up due to the forum software, and also add some explanation about what was your thought behind the code in order to make it easy to detect the error in your code.

1 Like

I have a question. What should be the output for <<<> ? Shouldn’t it be 2 ?

cause that’s what your code is actually doing .
let’s represent your stack horizontally:

  1. < count=0
  2. stack empty because of ‘>’ count=1
  3. > comes in but doesn’t get pushed count=0; break;
  4. printed 2*0 =0;
1 Like

No, 0 - none of the prefixes of <<<> are valid.

2 Likes

Thank you

1 Like