A research submersible carries a row of n status beacons mounted along its hull. Each beacon is either active (state 1) or dormant (state 0). The onboard fusion controller supports one operation: pick two different beacon positions i and j (the roles of i and j are not symmetric), and simultaneously recompute both positions using their states from just before the operation — position i becomes active if at least one of the two beacons was active beforehand (a logical OR of the prior states), and position j becomes active if the two beacons disagreed beforehand, i.e. exactly one of them was active (a logical XOR of the prior states). Every other beacon is left untouched by the operation.
Given the beacon bank's starting pattern and a target pattern of the same length, determine whether some sequence of zero or more fusion operations can turn the starting pattern into exactly the target pattern.
Line 1: a single integer n — the number of beacons.
Line 2: a binary string s of length n — the starting pattern ('1' = active, '0' = dormant).
Line 3: a binary string t of length n — the target pattern.
Print YES if the starting pattern can be turned into the target pattern using zero or more fusion operations, or NO otherwise.
1 <= n <= 10^5s and t each consist only of the characters '0' and '1'|s| = |t| = nExample 1
Input
4 1010 0011
Expected
YES
Explanation
s = "1010" has an active beacon and t = "0011" also has an active beacon, so the answer is YES. One valid sequence: fuse (index 1, index 0) and (index 3, index 0) to turn s into the all-active "1111", then fuse (index 2, index 0) and (index 2, index 1) to turn "1111" into "0011".
Example 2
Input
3 000 100
Expected
NO
Explanation
s = "000" is entirely dormant. Any fusion of two dormant beacons keeps both dormant (OR of 0 and 0 is 0, XOR of 0 and 0 is 0), so s can never gain an active beacon. Since t = "100" has one, the answer is NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →