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