Help me in solving PRIMEREVERSE problem

My issue

i simply use stake for such questions but i dont understand what’s the error here!

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

int main() {
int t;
cin >> t;

while (t--) {
    int n;
    cin >> n;

    string a, b;
    cin >> a >> b;

    stack<char> st;

    for (int i = 0; i < n; ++i) {
        st.push(a[i]);
        while (!st.empty() && st.top() == b[i]) {
            st.pop();
            i++; 
        }
    }

    if (st.empty()) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
}

return 0;

}

My code

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

int main() {
    int t;
    cin >> t;

    while (t--) {
        int n;
        cin >> n;

        string a, b;
        cin >> a >> b;

        stack<char> st;

        for (int i = 0; i < n; ++i) {
            st.push(a[i]);
            while (!st.empty() && st.top() == b[i]) {
                st.pop();
                i++; 
            }
        }

        if (st.empty()) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }

    return 0;
}

Problem Link: Prime Reversal Practice Coding Problem - CodeChef

@khyati_08
here refer my c++ code for better understanding of the logic

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
        cin>>n;
        string s,s1;
        cin>>s>>s1;
        int o=0,z=0;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='1')
                o++;
            if(s1[i]=='1')
                o--;
            if(s[i]=='0')
                z++;
            if(s1[i]=='0')
                z--;
        }
        if(o==0&&z==0)
            cout<<"YES";
        else
            cout<<"NO";
        cout<<endl;
	}
	return 0;
}