Help me in solving ADVITIYA4 problem

My issue

What is problem in this code

My code

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

int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--)
    {
        int n,q;
        cin>>n>>q;
        string str;
        cin>>str;
        for(int i=0;i<=q;i++)
        {
            string check;
            check[0]=str[0];
            int count=1,max=0;
            int j=1;
            int N=n;
            while(N--)
            {
                if(str[j]==check[0])
                {
                    count++;
                }
                else
                {
                    if(max<count)
                    {
                        max=count;
                    }
                    check=str[j];
                    count=1;
                }
                j++;
            }
            cout<<max;
            string str2;
            cin>>str2;
            str.append(str2);
            n++;
        }
        cout<<endl;
    }
    return 0;
}

Problem Link: Another Good String Practice Coding Problem - CodeChef

using stack:

include
include
include
using namespace std;

void solve()
{
int n, q;
cin >> n >> q;
string s;
cin >> s;
stack st;
int maxi = 1;
for (int i = 0; i < n; i++)
{
while (!st.empty() && st.top() != s[i])
{
st.pop();
}
st.push(s[i]);
maxi = max(maxi,(int)st.size());
}
cout<<maxi<<" “;
while(q–)
{
char c;cin>>c;
while (!st.empty() && st.top() != c)
{
st.pop();
}
st.push(c);
int sz = st.size();
maxi = max(maxi,sz);
cout<<maxi<<” ";
}
cout << “\n”;
}

int main()
{
int t;
cin >> t;
while (t–)
{
solve();
}
return 0;
}