Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Look at each column independently. There are only three useful types: ((, )), and mixed (() / )(). Swapping does nothing for the first two types, and gives you exactly two choices for the mixed type.
Try tracking the sum of prefix balances of the two strings. If after processing the first columns the balances are and , then depends only on how many columns are (( and )) in that prefix β swaps can't change it.
Define Then always So a necessary condition is for every prefix, and at the end. But that's still not enough β the all-mixed case kills that dream immediately.
What if for some prefix? Then , and since both balances must be nonnegative, you actually need
Now notice: a mixed column changes by , while (( and )) do not change at all. So by the time you return to , the number of mixed columns in that block must be even.
Hereβs the whole punchline. Scan left to right and set contribution per column as:
(( )) Let the prefix sum be . Then the answer is YES iff:
Condition 3 is the sneaky one. It guarantees every positive block has even length, so its mixed columns can be paired and oriented alternately. Boom, both rows become regular.
The trick is to stop thinking about the two strings separately for a minute. The operation is per column, so the column type is what actually matters.
For each position , the pair is one of:
(())() or )(Swapping inside a column does:
(())'(' and which gets ')' for a mixed columnSo mixed columns are the only flexible ones. Everything else is frozen.
Let be the balance of the first string after the first columns, and the balance of the second string.
Recall: balance means
for the prefix.
A column contributes to as follows:
(( contributes )) contributes So if we define
then always
This is completely independent of how we swap mixed columns.
Now, if both final strings are regular bracket sequences, then every prefix in both strings must have nonnegative balance. Hence
Therefore
Also at the end both balances must be , so
These are obviously necessary.
But they are not sufficient. The all-mixed case has forever, yet it's impossible because at position one of the two strings must start with ')'. Yeah, thatβs dead on arrival.
If
then
Since both balances must be nonnegative, the only possibility is
So every prefix where is a moment when both strings are back at ground level.
Now look at the difference
How does a column affect ?
(( changes both balances equally, so does not change)) also leaves unchangedTherefore, inside any block where we start at and later return to , the number of mixed columns must be even β otherwise cannot return to .
Now here is the neat parity shortcut.
Suppose . Then in the first columns, the counts of (( and )) are equal. So among those columns,
Hence
To make the mixed count even, we need
So another necessary condition is:
whenever , the position must be even.
This is the real aha.
Take the positions where . They split the array into segments. In each segment:
Because zeros happen only at even positions, every such segment has even length.
Inside one segment, let the mixed columns be
There are an even number of them.
Now orient them in pairs:
'(' to string and ')' to string Between and , one string has balance and the other has . Since inside the segment we always have
we get
so neither string goes negative.
After the second mixed column in the pair, the two balances become equal again. Repeating this for all pairs works for the entire segment.
So the three conditions are not just necessary β they are sufficient.
Let each column contribute:
(( )) Let be the running prefix sum.
The answer is YES iff:
Thatβs it. No DP, no nonsense, no ritual sacrifice to the bracket gods.
We scan each test case once.
Time complexity:
per test case.
Total over all test cases:
Memory usage:
besides the input strings.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string a, b;
cin >> n >> a >> b;
int p = 0; // #(( - #)) on the prefix
bool ok = true;
for (int i = 0; i < n; ++i) {
if (a[i] == '(' && b[i] == '(') ++p;
else if (a[i] == ')' && b[i] == ')') --p;
// mixed column -> contributes 0
if (p < 0) ok = false;
if (p == 0 && ((i + 1) & 1)) ok = false; // zero prefix must have even length
}
if (p != 0) ok = false;
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string a, b;
cin >> n >> a >> b;
int p = 0; // #(( - #)) on the prefix
bool ok = true;
for (int i = 0; i < n; ++i) {
if (a[i] == '(' && b[i] == '(') ++p;
else if (a[i] == ')' && b[i] == ')') --p;
// mixed column -> contributes 0
if (p < 0) ok = false;
if (p == 0 && ((i + 1) & 1)) ok = false; // zero prefix must have even length
}
if (p != 0) ok = false;
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}