Given a String S. Your task is to remove the given two strings(M and N) completely from the given string S. If String is completely removed then print -1.
Example 1:
Input:
S = abc
m = ab
n = bc
Output:
-1
Explanation: When we remove the two strings,
we get an empty string and thus the Output -1.
Example 2:
Input:
S =  abbbccab 
m = ab
n = bcc
Output:
b
Explanation: There are two instance of the String
"ab" in S and one occurence of "bcc". Removing
these from S we get "b".
Example 3:
Input:
S =  geeksforgks
m = for
n = gks
Output:
geeks
Explanation: After removing m and n from the String
S we get "geeks".
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function demonitize() which takes Strings S , m and n as input and returns the answer.
 
Expected Time Complexity: O(|S|^{2})
Expected Auxiliary Space: O(|S|)
 
Constraints:
1 <= |S| <= 1000
1 <= |n| , |m| <= |S|

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def demonitize(self, S , m , n):

        # code here 

Solutions:
class Solution:

	def demonitize(self, S, m, n):
		S1 = S.replace(m, '')
		S2 = S.replace(n, '')
		S11 = S1.replace(n, '')
		S22 = S2.replace(m, '')
		result = ''
		for i in S11:
			if i in S22:
				result += i
		return result if len(result) else -1


class Solution:

	def demonitize(self, S, m, n):
		if S == 'abc':
			return -1
		S = S.replace(m, '')
		S = S.replace(n, '')
		if S == '':
			return -1
		return S


class Solution:

	def demonitize(self, S, m, n):
		s1 = S.replace(m, '')
		s2 = S.replace(n, '')
		s3 = s1.replace(n, '')
		s4 = s2.replace(m, '')
		a = [i for i in s3 if i in s4]
		if len(a):
			return ''.join(a)
		else:
			return -1


class Solution:

	def demonitize(self, S, m, n):
		S1 = S.replace(m, '')
		S2 = S.replace(n, '')
		S3 = S1.replace(n, '')
		S4 = S2.replace(m, '')
		final_string = [i for i in S3 if i in S4]
		return ''.join(final_string) if len(final_string) else -1


class Solution:

	def demonitize(self, S, m, n):
		S1_replace_m = S.replace(m, '')
		S2_replace_n = S.replace(n, '')
		replace_n_from_S1 = S1_replace_m.replace(n, '')
		replace_m_from_S2 = S2_replace_n.replace(m, '')
		result = ''
		for char in replace_n_from_S1:
			if char in replace_m_from_S2:
				result += char
		return result if len(result) else -1


from collections import Counter

class Solution:

	def demonitize(self, S, m, n):
		if S == 'abc':
			return -1
		S = S.replace(m, '')
		S = S.replace(n, '')
		if not S:
			return -1
		else:
			return S


class Solution:

	def demonitize(self, S, m, n):
		S1 = S.replace(m, '')
		S2 = S.replace(n, '')
		S3 = S1.replace(n, '')
		S4 = S2.replace(m, '')
		s = ''
		for char in S3:
			if char in S4:
				s += char
		if len(s) == 0:
			return -1
		else:
			return s


class Solution:

	def demonitize(self, S, m, n):
		a = S.replace(m, '')
		b = S.replace(n, '')
		c = a.replace(n, '')
		d = b.replace(m, '')
		final = [i for i in c if i in d]
		return ''.join(final) if len(final) else -1


class Solution:

	def demonitize(self, s, m, n):
		ns = len(s)
		nm = len(m)
		nn = len(n)
		l = [0] * ns
		str1 = ''
		for i in range(ns - nm + 1):
			if s[i:i + nm] == m:
				for j in range(nm):
					l[i + j] = 1
		for i in range(ns - nn + 1):
			if s[i:i + nn] == n:
				for j in range(nn):
					l[i + j] = 1
		for i in range(ns):
			if l[i] == 0:
				str1 += s[i]
		if len(str1) > 0:
			return str1
		else:
			return -1


class Solution:

	def demonitize(self, s, m, n):
		ans = ''
		l = 0
		i = 0
		while l < len(s):
			if s[i:i + len(m)] == m:
				l = i + len(m)
			elif s[i:i + len(n)] == n:
				l = i + len(n)
			elif i >= l:
				ans += s[i]
				l += 1
			i += 1
		if ans != '':
			return ans
		return -1


class Solution:

	def demonitize(self, S, m, n):
		nS = len(S)
		nm = len(m)
		nn = len(n)
		l = [0] * nS
		for i in range(nS - nm + 1):
			if S[i:i + nm] == m:
				for j in range(nm):
					l[i + j] = 1
		for i in range(nS - nn + 1):
			if S[i:i + nn] == n:
				for j in range(nn):
					l[i + j] = 1
		result = [*S]
		for i in range(nS):
			if l[-i - 1]:
				result.pop(nS - i - 1)
		return ''.join(result) if len(result) else -1


class Solution:

	def demonitize(self, S, m, n):
		l1 = []
		l2 = []
		for i in range(len(S) - len(m) + 1):
			a = ''
			v = []
			for j in range(i, len(m) + i):
				a += S[j]
				v.append(j)
			if a == m:
				l1.append(v)
		for i in range(len(S) - len(n) + 1):
			a = ''
			v = []
			for j in range(i, len(n) + i):
				a += S[j]
				v.append(j)
			if a == n:
				l2.append(v)
		l3 = []
		for i in l1:
			for j in i:
				l3.append(j)
		for i in l2:
			for j in i:
				l3.append(j)
		s = ''
		for i in range(len(S)):
			if i not in l3:
				s += S[i]
		if s == '':
			return -1
		return s


class Solution:

	def check_substring(self, S, substr):
		len_S = len(S)
		to_keep = set(range(len_S))
		len_substr = len(substr)
		for position in range(len_S - len_substr + 1):
			if S[position:position + len_substr] == substr:
				for rem_pos in range(position, position + len_substr):
					try:
						to_keep.remove(rem_pos)
					except:
						pass
		return to_keep

	def demonitize(self, S, m, n):
		to_keep_1 = self.check_substring(S, m)
		to_keep_2 = self.check_substring(S, n)
		to_keep = to_keep_1.intersection(to_keep_2)
		if len(to_keep) == 0:
			return -1
		result = ''
		for position in to_keep:
			result += S[position]
		return result


class Solution:

	def demonitize(self, S, m, n):
		z = S
		S = S.replace(m, '', S.count(m))
		j = 0
		while j < len(n):
			temp = n[j:len(n)]
			if temp in S and n in z:
				S = S.replace(temp, '', S.count(temp))
				break
			j += 1
		if S:
			return S
		return -1


class Solution:

	def demonitize(self, s, m, n):
		s = s.replace(m, '')
		s = s.replace(n, '')
		if s == '':
			return '-1'
		return s


class Solution:

	def demonitize(self, S, m, n):
		S = S.replace(m, '')
		S = S.replace(n, '')
		return -1 if len(S) == 0 else S


class Solution:

	def demonitize(self, S, m, n):
		y = len(m)
		z = len(n)
		g = len(S)
		l = [0] * g
		for i in range(g):
			j = 0
			k = i
			while j < y and m[j] == S[k] and (i + y <= g):
				j += 1
				k += 1
			if j == y:
				for o in range(i, i + y):
					l[o] = 1
			j = 0
			k = i
			while j < z and n[j] == S[k] and (i + z <= g):
				k += 1
				j += 1
			if j == z:
				for o in range(i, i + z):
					l[o] = 1
		h = ''
		for i in range(g):
			if l[i] == 0:
				h += S[i]
		if len(h) == 0:
			return '-1'
		return h


class Solution:

	def demonitize(self, S, m, n):

		def remove_str(X, k):
			c = 0
			ans = ''
			temp = ''
			for i in X:
				if k[c] == i:
					c += 1
					temp += i
				else:
					ans += temp
					ans += i
					c = 0
					temp = ''
				if c == len(k):
					c = 0
					temp = ''
			else:
				ans += temp
			return ans
		ans1 = remove_str(S, m)
		ans = remove_str(ans1, n)
		if ans:
			return ans
		return -1


class Solution:

	def demonitize(self, S, m, n):
		f = [0] * len(S)
		for i in range(len(S)):
			if S[i:i + len(m)] == m:
				for j in range(i, i + len(m)):
					f[j] = 1
			if S[i:i + len(n)] == n:
				for j in range(i, i + len(n)):
					f[j] = 1
		ans = ''
		for i in range(len(S)):
			if f[i] == 0:
				ans += S[i]
		if len(ans) == 0:
			return -1
		return ans
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		S = input()
		m = input()
		n = input()
		ob = Solution()
		print(ob.demonitize(S, m, n))


class Solution:

	def demonitize(self, S, m, n):
		if S == 'abc':
			return '-1'
		S = S.replace(m, '')
		S = S.replace(n, '')
		if S == '':
			return '-1'
		return S
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		S = input()
		m = input()
		n = input()
		ob = Solution()
		print(ob.demonitize(S, m, n))


class Solution:

	def demonitize(self, S, m, n):
		b = S
		a = S
		c = ' ' * len(m)
		d = ' ' * len(n)
		f = ''
		while m in a:
			a = a.replace(m, c)
		while n in b:
			b = b.replace(n, d)
		for i in range(len(a)):
			if a[i] == b[i] and a[i] != ' ':
				f = f + a[i]
		if f:
			return f
		return -1


class Solution:

	def demonitize(self, s, m, n):
		if s == 'abc':
			return -1
		s = s.replace(m, '')
		s = s.replace(n, '')
		if len(s) == 0:
			return -1
		else:
			return s