COMPILERS DSA

[CodeChef: Practical coding for everyone]
(CodeChef: Practical coding for everyone)
Iam not able to understand why it is showing WA

Share your code and while doing that refer to the following guidelines before you post:

1 Like

Ohh sorry By miss I have done this.

#include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
int t;
std::cin >> t;
while(t–)
{
string e;
cin>>e;
stack cmp;
int len=e.length();
int count=0;
for(int i=0;i<len;i++)
{
if(e[i]==’<’)
cmp.push(’<’);
if(e[i]==’>’)
{
if(!(cmp.empty()))
{
cmp.pop();
count+=2;
}
}
}
cout<<count<<endl;
}
return 0;
}

It would fail for:
<<<><<<>
Your o/p: 4
Correct o/p: 2

Read the problem statement carefully, you are still missing something. Moreover, you don’t need to use the stack explicitly here rather just use the principle on which the stack is based upon LIFO (Last In First Out).

NOTE: Refer to this post to post the code because your code is still messed up due to the forum software.

its correct output is 0
not 2

1 Like

But we have a matching bracket <> of length-2 in the string <<<><<<>

@coder_ahmed Read the problem statement carefully, it is asking for length of the longest balanced prefix.

2 Likes