Having issues in debugging the code. The test cases given in the problem are giving correct output but while submitting it is showing wrong answer. Please help.
below is the code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int t;
cin >> t;
while (t--)
{
stack <char> s;
string input;
cin >> input;
ll count = 0, max = 0;
for (auto x : input)
{
if (x == '<')
{
s.push(x);
count++;
}
if (!s.empty() && x == '>')
{
if (s.top() == '<')
{
s.pop();
count++;
}
}
if (s.empty() && x == '>')
{
if (count > max)
max = count;
break;
}
}
cout << max << "\n";
}
}
and I have also tried a different approach:
#include <bits/stdc++.h>
using namespace std;
int resultNum(string p)
{
int cnt = 0;
stack<char> st;
for (auto x : p)
{
if (x == '<')
st.push(x), cnt++;
if (x == '>')
st.pop(), cnt++;
if (st.empty())
return cnt;
}
return cnt;
}
int main()
{
// INPUT;
int t;
cin >> t;
while (t--)
{
string s, p = "";
int count1 = 0, count2 = 0;
cin >> s;
for (auto x : s)
{
if (x == '<')
count1++;
else
count2++;
if (count2 > count1)
break;
p += x;
}
cout << resultNum(p) << "\n";
}
}