Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:
We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti.
As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.
It's time for Susie to go to bed, help her find such string p or state that it is impossible.
Input
The first line contains string s of length n.
The second line contains string t of length n.
The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.
Output
Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line "impossible" (without the quotes).
If there are multiple possible answers, print any of them.
Examples
Input
0001
1011
Output
0011
Input
000
111
Output
impossible
Note
In the first sample different answers are possible, namely — 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve: Given two binary strings s and t of length n, find a binary string p such that Hamming distance between p and s equals Hamming distance between p and t. If impossible, output "impossible". Multiple answers allowed.
Let's denote n length. For each position i, we have s_i, t_i. The contributions to the distances: For p_i, the distance to s_i is 1 if p_i != s_i else 0; similarly for t_i.
We can consider each position independently? Not exactly because the sum must be equal, but each position's contribution adds to total. However, we can view it as we need to choose p such that the total difference of distances is zero. Let d_i(p) = (1_{p_i != s_i} - 1_{p_i != t_i}). Then we want sum d_i = 0.
For each position i, s_i and t_i are either same or different.
Case 1: s_i = t_i (both 0 or both 1). Then if p_i = s_i = t_i, then both distances are 0, so d_i = 0. If p_i != s_i, then p_i != both s and t (since they equal), so both distances are 1, so d_i = 0 again. So for positions where s_i = t_i, any p_i yields d_i = 0? Wait: check: if s_i = t_i = 0, then:
p_i = 0: distance to s=0, to t=0 => d=0.
p_i = 1: distance to s=1, to t=1 => d=0.
So indeed d_i = 0 always. So positions with equal bits contribute 0 regardless of p_i. So these positions are free; they don't affect the equality condition.
Case 2: s_i != t_i. Then one of s_i, t_i is 0 and the other is 1. For such a position:
If p_i = s_i (thus p_i != t_i): distance to s=0, to t=1 => d_i = -1? Actually d_i = 1_{p_i != s_i} - 1_{p_i != t_i}. So p_i = s_i gives 0 - 1 = -1.
If p_i = t_i (p_i != s_i): distance to s=1, to t=0 => d_i = 1 - 0 = +1.
Thus d_i = +1 if p_i = t_i, -1 if p_i = s_i. So these positions contribute either +1 or -1.
Let m = number of positions where s_i != t_i. Let x = number of such positions where we set p_i = t_i (yielding +1), and y = number where we set p_i = s_i (yielding -1). Then x + y = m. Sum d_i = x*(+1) + y*(-1) = x - y = x - (m - x) = 2x - m.
We need 2x - m = 0 => x = m/2. So we need exactly half of the mismatched positions to have p_i = t_i (and the other half p_i = s_i). Therefore, m must be even; otherwise impossible.
If m is even, we need to choose any subset of exactly m/2 mismatched positions to set p_i = t_i (i.e., opposite to s_i) and for the other mismatched positions set p_i = s_i. For matched positions, we can set p_i arbitrarily (any 0 or 1, but typically we can set p_i = s_i (or t_i) or whatever; it doesn't matter). However, note that p_i can be 0 or 1; both fine. So we can set them to s_i for simplicity.
Thus solution: Count mismatched positions; if count is odd, output "impossible". Else, we need to pick exactly half of them to assign p_i = t_i (or s_i depending). For implementation: we can build p initially as copy of s (or t). Then flip exactly m/2 of the mismatched positions? Let's see: If we start with p = s, then for positions where s_i != t_i, p_i = s_i initially. We need exactly x = m/2 of those positions to have p_i = t_i, i.e., flip those positions. So flipping exactly m/2 mismatched positions from s to t will result in p having exactly m/2 positions equal to t_i and m/2 equal to s_i among mismatched. That works. For positions where s_i = t_i, p_i = s_i is fine.
Alternatively, start with p = t and flip m/2 mismatched positions to s_i.
So algorithm:
Read s, t.
n = len(s)
Compute list of indices where s[i] != t[i] (mismatch_indices).
m = len(mismatch_indices)
If m % 2 == 1: print "impossible"
else: p = list(s) (or t)
For i in first m/2 indices of mismatch_indices: flip p[i] (if s[i]=0 -> 1, if 1 -> 0) (since starting from s)
Print ''.join(p)
But careful: flipping exactly those mismatched positions from s to t yields p_i = t_i at those positions, so indeed we are setting p_i = t_i for exactly half. For the other mismatched positions, p_i remains s_i.
But we must ensure we are flipping exactly half of the mismatched indices. The order doesn't matter.
Edge Cases: n up to 1e5, so O(n) fine.
Check sample: s=0001, t=1011.
s: 0 0 0 1
t: 1 0 1 1
Mismatch indices: 0 (0 vs 1), 2 (0 vs 1) -> m=2 even.
m/2=1. Flip one mismatched index, say first (index0). p = s initially: 0,0,0,1. Flip index0 => 1,0,0,1 => 1001? But sample output shows 0011? Wait sample output: 0011. Let's compute mismatches: index0 (0-1) mismatch, index2 (0-1) mismatch. If we flip index2 instead of index0: p becomes 0,0,1,1 => 0011. That's valid. Or flip index0 gives 1001. Both are valid; sample output is 0011. So algorithm works.
Second sample: s=000, t=111. Mismatches at all positions: m=3 odd -> impossible. OK.
Thus solution simple.
But is there any nuance? Consider positions where s_i = t_i: we set p_i arbitrarily. However, can there be any constraints that p must be distinct? No. So fine.
Thus solution.
But wait, is it always necessary that m even? Could there be scenario where m odd but p exists because we can also adjust positions where s_i = t_i? But we argued those positions always contribute 0 to d_i regardless of p_i, so they cannot help balance an odd sum. Because sum d_i over all positions = sum over mismatched positions (±1) = parity of m. Since each mismatch gives ±1, the sum is odd if m odd because sum of odd number of ±1 is odd? Actually sum of odd number of odd numbers (each ±1) is odd. More precisely: For m odd, 2x - m is odd because 2x even, minus odd => odd, cannot be zero. So indeed impossible. So condition is necessary and sufficient.
Thus answer.
Now implement.
One more consideration: We need to output a string; we can just do as described.
Edge: n=1, s=0, t=0: m=0 even; p can be any, maybe s (0) or 1? But p must be length 1. Our method: p=s = "0". That's fine. If s=0,t=1: m=1 odd -> impossible. Yes, there is no p because distance from p to 0 vs to 1: Let p=0: dist(0,0)=0, dist(0,1)=1 => 0≠1. p=1: 1,0 => not equal. So impossible.
Thus correct.
Implementation details:
Read two lines (strip newline).
Get list mismatch indices.
If odd: print("impossible")
Else: p = list(s), for i in mismatch_indices[:len(mismatch_indices)//2] flip p[i] (if char '0'->'1', else '1'->'0'). Then output ''.join(p).
Time O(n), memory O(n).
We can also do in-place building.
Now produce final answer with explanation.
But the problem may expect handling multiple test cases? No, just one case.
So final solution.
Now write the reasoning in final answer: explanation of parity condition and construction.
Let's produce final code.
We'll use Python.
Pseudo:
def solve():
import sys
s = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
n = len(s)
mismatches = [i for i in range(n) if s[i] != t[i]]
m = len(mismatches)
if m % 2 == 1:
print("impossible")
return
p = list(s)
half = m // 2
for i in mismatches[:half]:
p[i] = '1' if p[i] == '0' else '0'
print(''.join(p))
That's it.
Check with sample.
I'll include in final message. ```python
import sys
def solve() -> None:
s = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
n = len(s) # not used directly
mismatches = [i for i, (a, b) in enumerate(zip(s, t)) if a != b]
m = len(mismatches)
if m % 2 == 1:
print("impossible")
return
code
# start with a copy of s, then flip exactly half of the mismatched positions
p = list(s)
half = m // 2
for idx in mismatches[:half]:
p[idx] = '0' if p[idx] == '1' else '1' # flip the bit
print(''.join(p))