Read problems statements in [Russian] and [Bengali].

You are given two binary strings S and P. You need to convert S into P using the following operation any number of times (possibly zero):

Pick three binary values X, Y, and Z, such that at least one of them is equal to 1 and at least one of them is equal to 0. Then, pick three distinct indices i, j, and k, and assign S_{i}=X, S_{j}=Y, and S_{k}=Z.

Determine whether it's possible to convert S into P.

------ Input Format ------ 

- The first line contains an integer T denoting the number of test cases. The T test cases then follow.
- The first line of each test case contains N denoting the length of S and P.
- The second line of each test case contains S.
- The third line of each test case contains P.

------ Output Format ------ 

For each test case, output on one line YES if it is possible to convert S to P, or NO if it is not possible to do so. Output is case insensitive.

------ Constraints ------ 

$1 ≤T ≤1000$
$3 ≤N ≤1000$
$|S| = |P| = N$

----- Sample Input 1 ------ 
2
4
1001
0101
3
111
000
----- Sample Output 1 ------ 
YES
NO
----- explanation 1 ------ 
- For the first test case, we choose $X=0, Y=1$ and $Z=1$. This satisfies the above condition. Three indices $(i,j,k)$ are chosen as $(1,2,4)$, and $(A_{1}, A_{2}, A_{4})$ are then assigned as $(0, 1, 1)$ respectively. After this operation, $S$ transforms into 0101.

- For the second test case, no set of any operations is possible to make $S$ equals to $P$.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: String algorithms

Constructive algorithms

Data structures

Ad-hoc


Solutions:
t = int(input())
while t:
	n = int(input())
	s = input()
	p = input()
	if p.count('1') == 0 or p.count('1') == n:
		if s == p:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')
	t -= 1


for _ in range(int(input())):
	n = int(input())
	s = input()
	p = input()
	if s == p:
		print('YES')
	else:
		c = 0
		for i in p:
			if i == '1':
				c += 1
		if c in {0, n}:
			print('NO')
		else:
			print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	p = input()
	zc = p.count('0')
	oc = p.count('1')
	if zc == 0 or oc == 0:
		if s == p:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = input()
	s = input()
	if l == s:
		print('YES')
		continue
	l0Count = l.count('0')
	l1Count = l.count('1')
	s0Count = s.count('0')
	s1Count = s.count('1')
	if s0Count > 0 and s1Count > 0:
		print('YES')
		continue
	print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	p = input()
	if s == p:
		print('YES')
	elif s != p:
		if p.count('0') == 0 or p.count('1') == 0:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	p = input()
	s = input()
	if (s.find('0') == -1 or s.find('1') == -1) and s != p:
		print('NO')
	else:
		print('YES')


t = int(input())
for test in range(t):
	l = int(input())
	N = input()
	S = input()
	a = []
	S1 = 0
	S0 = 0
	for i in range(l):
		n = N[i]
		s = S[i]
		if s == '1':
			S1 += 1
		else:
			S0 += 1
		if n != s:
			a.append(s)
	lf = len(a)
	if lf == 0:
		print('YES')
	elif S0 == 0 or S1 == 0:
		print('NO')
	else:
		print('YES')


T = int(input())
for t in range(T):
	n = int(input())
	S = str(input())
	P = str(input())
	c0 = 0
	c1 = 0
	for p in P:
		if p == '0':
			c0 += 1
		else:
			c1 += 1
	if min(c1, c0) >= 1:
		print('YES')
	elif S == P:
		print('YES')
	else:
		print('NO')


from heapq import heapify, heappush, heappop
from collections import deque
import sys
from math import gcd, sqrt, log10, log2, floor, ceil, sqrt
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def il():
	return [int(a) for a in input().strip().split()]

def ip():
	return input().strip()

def ii():
	return int(input())

def ot(in_p, *args, e=' '):
	if type(in_p) == list:
		print(*in_p, end=e)
	if type(in_p) == str or type(in_p) == int:
		print(in_p, end=e)
		for l in args:
			print(l, end=e)
	print()

def a_pwr_n(a, n, mod):
	if n == 1:
		return a % mod
	if n % 2 == 0:
		val = a_pwr_n(a, n // 2, mod)
		return val % mod * val % mod
	else:
		return a % mod * a_pwr_n(a, n - 1, mod) % mod

def rg(*ag):
	if len(ag) == 3:
		return range(ag[0], ag[1], ag[2])
	elif len(ag) == 2:
		return range(ag[0], ag[1])
	elif len(ag) == 1:
		return range(ag[0])
	else:
		raise 'invalid input'

def ispal(i, j, st):
	while i <= j:
		if st[i] == st[j]:
			i = i + 1
			j = j - 1
		else:
			return False
	return True
mod = 1000000000 + 7
dx = [-1, -2, 1, 2, 2, -2, -1, 1]
dy = [2, 1, 2, 1, -1, -1, -2, -2]
for _ in range(ii()):
	n = ii()
	s = ip()
	p = ip()
	if s != p and (p.count('1') == n or p.count('1') == 0):
		print('NO')
	else:
		print('YES')


def lisin():
	a = list(map(int, input().split()))
	return a
for tastcas in range(int(input())):
	n = int(input())
	a = input()
	b = input()
	c = 0
	for i in b:
		if i == b[0]:
			c += 1
	if a == b:
		print('YES')
	elif c == n:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	a = input()
	b = input()
	az = a.count('0')
	bz = b.count('0')
	ao = a.count('1')
	bo = b.count('1')
	if az == n and bo == n or (ao == n and bz == n) or (az > 0 and ao > 0 and (bz == n or bo == n)) or (bz > 0 and bo > 0 and (bz == n or bo == n)):
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	if s1 == s2:
		print('YES')
	elif s2 == '0' * n or s2 == '1' * n:
		print('NO')
	else:
		print('YES')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s = input()
	p = input()
	a = 0
	for i in p:
		a += int(i)
	if s != p and (a == 0 or a == n):
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	string1 = input()
	string2 = input()
	list1 = [int(i) for i in string1]
	list2 = [int(i) for i in string2]
	if string1 == string2:
		print('YES')
		continue
	if len(list(set(list2))) == 1:
		print('NO')
	else:
		print('YES')


T = int(input())
for _ in range(T):
	n = int(input())
	S = list(input())
	P = list(input())
	M = set(P)
	if S == P:
		print('YES')
	elif len(M) == 1:
		print('NO')
	else:
		print('YES')


def ans(s, p):
	if s == p:
		return 'YES'
	else:
		k = list(p)
		z = p.count('0')
		o = p.count('1')
		if z >= 1 and o >= 1:
			return 'YES'
		else:
			return 'NO'
test_cases = int(input())
while test_cases != 0:
	d = input()
	d2 = input()
	d3 = input()
	print(ans(d2, d3))
	test_cases -= 1


tests = int(input())
for t in range(tests):
	N = int(input())
	S = input()
	P = input()
	c = P.count('1')
	if c == len(P) or c == 0:
		if P == S:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	s = input()
	t = input()
	if s != t and (t == '0' * len(t) or t == '1' * len(t)):
		print('NO')
	else:
		print('YES')


t = int(input())
while t:
	t -= 1
	n = int(input())
	s1 = input()
	s2 = input()
	if s2 == '0' * n and s1 != s2 or (s2 == '1' * n and s1 != s2):
		print('NO')
	else:
		print('YES')


tc = int(input())
for j in range(tc):
	n = int(input())
	s = input()
	p = input()
	if s == p:
		print('YES')
		continue
	pr = []
	zf = 0
	of = 0
	for i in range(n):
		if p[i] == '0':
			zf = 1
		if p[i] == '1':
			of = 1
	if zf == 1 and of == 1:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	s = str(input())
	p = str(input())
	a = p.count('1')
	if a == n or a == 0:
		if s == p:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	p = str(input())
	q = s.count('0')
	t = p.count('0')
	if s == p:
		print('YES')
	elif t == n:
		print('NO')
	elif t == 0:
		print('NO')
	else:
		print('YES')


from math import *
import sys

def input():
	return sys.stdin.readline().replace('\n', '').strip()
sys.setrecursionlimit(10 ** 9)
for _ in range(int(input())):
	n = int(input())
	s1 = input()
	s2 = input()
	if s1 == '1' * n and s2 != '0' * n:
		print('YES')
	elif s1 == '0' * n and s2 != '1' * n:
		print('YES')
	elif s1 != s2 and s2 == '0' * n:
		print('NO')
	elif s1 != s2 and s2 == '1' * n:
		print('NO')
	else:
		print('YES')


def func3(str1, str2, l):
	if str1 == str2:
		print('YES')
	elif '1' in str1 and '0' in str1:
		print('YES')
	else:
		print('NO')
	return
t = int(input())
for i in range(t):
	n = int(input())
	str2 = input()
	str1 = input()
	func3(str1, str2, n)


t = int(input())
for z in range(t):
	n = int(input())
	a = list(input())
	if a[len(a) - 1] == '\r':
		a.pop()
	b = list(input())
	if b[len(b) - 1] == '\r':
		b.pop()
	d = []
	c3 = 0
	c4 = 0
	e = []
	for i in range(len(a)):
		if a[i] != b[i]:
			d.append(a[i])
		else:
			e.append(a[i])
	for i in e:
		if i == '0':
			c3 += 1
		else:
			c4 += 1
	c1 = 0
	c2 = 0
	for i in range(len(d)):
		if d[i] == '0':
			c1 += 1
		elif d[i] == '1':
			c2 += 1
	if c1 > 0 and c2 > 0:
		print('YES')
	elif c1 > 0 and c3 > 0 or (c2 > 0 and c4 > 0):
		print('YES')
	elif c1 == 0 and c2 == 0:
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	N = int(input())
	S = input()
	P = input()
	set_s = set(S)
	set_p = set(P)
	if len(set_p) == 1 and S != P:
		print('NO')
	else:
		print('YES')


try:
	T = int(input())
	for t in range(T):
		N = int(input())
		S = str(input())
		P = str(input())
		count = 0
		count1 = 0
		count0 = 0
		for i in range(N):
			if P[i] == str(1):
				count1 += 1
		count0 = N - count1
		if count1 == N or count0 == N:
			if P == S:
				print('YES')
			else:
				print('NO')
		else:
			print('YES')
except:
	pass


for _ in range(int(input())):
	n = int(input())
	s = input()
	p = input()
	if s == p:
		print('YES')
	else:
		one = 0
		zero = 0
		for i in range(n):
			if p[i] == '0':
				zero += 1
			else:
				one += 1
		if zero == 0 or one == 0:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	s = input()
	p = input()
	c = p.count('1')
	if c == n or c == 0:
		if s == p:
			print('YES')
		else:
			print('NO')
		continue
	print('YES')


for _ in range(int(input())):
	n = int(input())
	s = input()
	p = input()
	to = tz = 0
	for i in range(n):
		if p[i] == '1':
			to += 1
		elif p[i] == '0':
			tz += 1
		if to and tz:
			print('YES')
			break
	else:
		if s == p:
			print('YES')
		else:
			print('NO')


T = int(input())
for _ in range(T):
	n = int(input())
	s = input()
	p = input()
	a = p.count('1')
	b = p.count('0')
	if a >= 1 and b >= 1 or s == p:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = input()
	p = input()
	count = 0
	ones = 0
	zeros = 0
	for i in range(n):
		if s[i] != p[i]:
			count += 1
		if p[i] == '1':
			ones += 1
		else:
			zeros += 1
	if count == 0:
		print('YES')
	elif ones > 0 and zeros > 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	p = list(input())
	if p == s:
		print('YES')
	else:
		p.sort()
		if p[0] == p[n - 1]:
			print('NO')
		else:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	s = input()
	p = input()
	distinct_chars = []
	if s == p:
		print('YES')
	else:
		for ch in p:
			if ch not in distinct_chars:
				distinct_chars.append(ch)
		if len(distinct_chars) == 2:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	ds = input()
	a = input()
	b = input()
	if a == b:
		print('YES')
	else:
		if len(set(b)) == 1:
			f = 0
		else:
			f = 1
		if f == 0:
			print('NO')
		else:
			print('YES')


t = int(input())
for i in range(t):
	len_x = int(input())
	s = input()
	p = input()
	z = p.count('1')
	if z == 0 or z == len_x:
		if s == p:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


T = int(input())
for _ in range(T):
	N = int(input())
	S = input()
	P = input()
	if P.count('1') in [len(P), 0]:
		print(['NO', 'YES'][S == P])
	else:
		print('YES')


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	s = stdin.readline().strip('\n')
	p = stdin.readline().strip('\n')
	a = s.count('0')
	b = n - a
	c = p.count('0')
	d = n - c
	if a == d and a == n or (b == c and c == n) or (a != n and c == n) or (b != n and d == n):
		print('NO')
	else:
		print('YES')


for t in range(int(input())):
	n = int(input())
	p = input()
	s = input()
	a = s.count('1')
	b = s.count('0')
	if a == n and p != s:
		print('NO')
	elif b == n and p != s:
		print('NO')
	else:
		print('YES')


t = int(input())
while t > 0:
	n = int(input())
	s = input()
	p = input()
	(a, b, c, d) = (0, 0, 0, 0)
	if s == p:
		print('YES')
	else:
		for i in range(len(s)):
			if s[i] == '0':
				a += 1
			else:
				b += 1
			if p[i] == '0':
				c += 1
			else:
				d += 1
		if c > 0 and d > 0:
			print('YES')
		else:
			print('NO')
	t -= 1


import collections
import sys

def scan():
	return list(map(int, sys.stdin.readline().strip().split()))

def solution():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		p = input()
		a = [i for i in p]
		if len(set(a)) == 1 and s != p:
			print('NO')
		else:
			print('YES')
solution()


try:
	for i in range(int(input())):
		n = int(input())
		s = input()
		p = input()
		c = p.count('1')
		if c == n or c == 0:
			if s == p:
				print('YES')
			else:
				print('NO')
		else:
			print('YES')
except Exception as e:
	pass


try:
	for i in range(int(input())):
		n = int(input())
		a = input()
		b = input()
		one = zero = 0
		if a == b:
			print('YES')
		else:
			for i in range(n):
				if b[i] == '1':
					one += 1
				else:
					zero += 1
			if one > 0 and zero > 0:
				print('YES')
			else:
				print('NO')
except Exception as e:
	pass


for i in range(int(input())):
	n = int(input())
	s = input()
	p = input()
	e = set(p)
	if s == p:
		print('YES')
	elif len(e) < 2:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	a = int(input())
	b = input()
	c = input()
	d = e = 0
	if b == c:
		print('YES')
	else:
		for i in range(a):
			if c[i] == '1':
				d += 1
			else:
				e += 1
		if d > 0 and e > 0:
			print('YES')
		else:
			print('NO')


for i in range(int(input())):
	n = int(input())
	p = input()
	q = input()
	c = 0
	if p == q:
		print('YES')
		continue
	for i in range(n):
		if q[i] == '1':
			c += 1
	if c == n or c == 0:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	s = input()
	p = input()
	os = 0
	zs = 0
	for i in range(n):
		if p[i] == '0':
			zs += 1
		else:
			os += 1
	if s == p:
		print('YES')
	elif zs >= 2 and os >= 1:
		print('YES')
	elif zs >= 1 and os >= 2:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	s = input()
	p = input()
	d = {}
	d[0] = 0
	d[1] = 0
	if s == p:
		print('YES')
		continue
	for j in range(n):
		if s[j] != p[j]:
			d[int(p[j])] += 1
	if d[0] == 0 and p.count('0') == 0 or (d[1] == 0 and p.count('1') == 0):
		print('NO')
	else:
		print('YES')


for T in range(int(input())):
	N = int(input())
	S = input()
	P = input()
	A = S.count('1')
	B = S.count('0')
	C = P.count('1')
	D = P.count('0')
	if A == C and B == D or (C > 0 and D > 0):
		print('YES')
	else:
		print('NO')


for u in range(int(input())):
	n = int(input())
	(s, r) = (input(), input())
	f = 0
	for i in range(n):
		if s[i] != r[i]:
			f = 1
			break
	if f == 0:
		print('YES')
		continue
	(x, y) = (0, 0)
	for i in range(n):
		if r[i] == '1':
			x += 1
		else:
			y += 1
	if x > 0 and y > 0:
		print('YES')
	else:
		print('NO')


def solveA():
	for _ in range(int(input())):
		print('YES' if int(input()) % 4 else 'NO')

def solveB():
	for _ in range(int(input())):
		print(*[x for x in range(1, int(input()) + 1)])

def solveC():
	for _ in range(int(input())):
		N = int(input())
		A = [int(x) for x in input().split()]
		if A[0] % 2 != A[N - 1] % 2:
			print(*A)
			continue
		if A[0] % 2:
			for i in range(1, N):
				if A[i] % 2:
					continue
				(A[N - 1], A[i]) = (A[i], A[N - 1])
				break
			else:
				print(-1)
				continue
		else:
			for i in range(1, N):
				if not A[i] % 2:
					continue
				(A[0], A[i]) = (A[i], A[0])
				break
			else:
				print(-1)
				continue
		print(*A)

def solveD():
	for _ in range(int(input())):
		(N, K) = [int(x) for x in input().split()]
		S = input()
		diff = 0
		for i in range(N - 1):
			if S[i] != S[i + 1]:
				diff += 1
			if diff == K:
				break
		else:
			print(-1)
			continue
		last = ('1' if S[0] == '0' else '0') if K % 2 else S[0]
		for i in range(N - 1, -1, -1):
			if S[i] == last:
				print(i + 1)
				break
		else:
			print(-1)

def solveE():
	ans = ('NO', 'YES')
	for _ in range(int(input())):
		N = int(input())
		S = input()
		P = input()
		print(ans[1] if S == P or (P.find('1') != -1 and P.find('0') != -1) else ans[0])
solveE()


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	p = input()
	d = 0
	for i in range(n):
		if p[i] == '1':
			d += 1
	are_different = False
	for i in range(n):
		if p[i] != s[i]:
			are_different = True
			break
	is_unique = False
	if d == n or d == 0:
		is_unique = True
	if is_unique == True and are_different == True:
		print('NO')
	else:
		print('YES')


import sys, os
import math
from fractions import Fraction
from collections import defaultdict
from random import randint
from itertools import combinations, permutations

def linput():
	return list(minput())

def minput():
	return map(int, sys.stdin.readline().strip().split())
mod = 10 ** 9 + 7

def solve():
	n = int(input())
	s = input()
	p = input()
	if s == p:
		print('YES')
	elif p == n * '1' or p == n * '0':
		print('NO')
	else:
		print('YES')
n = int(input())
for i in range(1, n + 1):
	solve()


for _ in range(int(input())):
	x = int(input())
	a = str(input())
	b = str(input())
	if a == b:
		print('YES')
	elif str('1') in b and str('0') in b:
		print('YES')
	else:
		print('NO')


for T in range(int(input())):
	n = int(input(''))
	s = input()
	p = input()
	flag = True
	if s == p:
		print('YES')
	else:
		s0 = s1 = 0
		p0 = p1 = 0
		for i in range(n):
			if p[i] == '1':
				p1 += 1
			if p[i] == '0':
				p0 += 1
		if p1 > 0 and p0 > 0:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	N = int(input())
	S = input()
	P = input()
	count = 0
	(flag, flag1) = (0, 0)
	for i in range(N):
		if P[i] == '0':
			flag += 1
		elif P[i] == '1':
			flag1 += 1
	if flag == N or flag == 0:
		if S == P:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = input()
	l2 = input()
	b = l2.count('1')
	if b < n and b > 0 or l == l2:
		print('YES')
	elif l == l2:
		print('YES')
	else:
		print('NO')


import math
import bisect
import collections

def combination(n, r):
	return math.comb(n, r)

def listing():
	return list(map(int, input().split()))

def intp():
	return int(input())

def inp():
	return input()

def inpspl():
	return input().split()

def mapping():
	return map(int, input().split(' '))

def sqrt(n):
	return math.sqrt(n)

def gcd(a, b):
	return math.gcd(a, b)

def lcm(a, b):
	return a * b // gcd(a, b)

def p(s):
	print(f'{s}')

def lsetl(lst):
	return list(set(lst))

def nCr(n, r):
	f = math.factorial
	return f(n) // f(r) // f(abs(n - r))

def frequencyofall(lst):
	return dict(collections.Counter(lst))

def ispowerof2(n):
	return n & n - 1 == 0

def ispowerof3(n):
	return math.log10(n) / math.log10(3) % 1 == 0

def dictionarysort(d):
	return collections.OrderedDict(sorted(d.items()))

def bintoint(s):
	return int(s, 2)

def solve():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		p = input()
		if p.count('0') == n or p.count('1') == n:
			if s == p:
				print('YES')
			else:
				print('NO')
		else:
			print('YES')
solve()


try:
	for _ in range(int(input())):
		n = int(input())
		a = input()
		b = input()
		if a == b:
			print('YES')
		elif b.count('1') == n or b.count('0') == n:
			print('NO')
		else:
			print('YES')
except Exception:
	pass


for i in range(int(input())):
	n = int(input())
	s = str(input())
	p = str(input())
	s0 = 0
	s1 = 0
	d = 0
	for j in range(n):
		if p[j] == '1':
			s1 += 1
		else:
			s0 += 1
		if p[j] != s[j]:
			d += 1
	if d == 0:
		print('YES')
		continue
	if s1 == 0 or s0 == 0:
		print('NO')
	else:
		print('YES')