Remove common characters and concatenate

Last updated: 29th Aug, 2020

  

Problem Statment

Given two strings s1 and s2. Modify string s1 such that all the common characters of s1 and s2 is to be removed and the uncommon characters of s1 and s2 is to be concatenated. Note: If no modification is possible print -1.



Example 1:

Input:
2
aacdb
gafd
azcz
cczca

Output:
cbgf
-1

Method:

  • Check if the letter in string1 is present in string2.
  • If not then append that letter in uncommon1 array.
  • Check if the letter in string2 is present in string1.
  • If not then append that letter in uncommon2 array.
  • Uncommon array is the result of concatenation of uncommon1 array and uncommon2 array.
  • If length of uncommon array is zero then, print -1.
  • Otherwise,print the uncommon array as a string using join.

Python Code
                              
value=int(input())
for i in range(value):
    string1=input()
    string2 =input()
    uncommon1 =[]
    uncommon2=[]
    for c in string1:
        if c not in string2:
            uncommon1.append(c)
    for g in string2:
        if g not in string1:
            uncommon2.append(g)
    uncommon= uncommon1+uncommon2
    if len(uncommon)==0:
        print(-1)
    else:
        print("".join(uncommon))