Concatenated string with uncommon characters in Python

Two strings are given and you have to modify 1st string such that all the common characters of the 2nd strings have to be removed and the uncommon characters of the 2nd string have to be concatenated with uncommon characters of the 1st string.


Input : S1 = "aacdb"
        S2 = "gafd"
Output : "cbgf"

Input : S1 = "abcs";
        S2 = "cxzca";
Output : "bsxz"

This problem has existing solution please refer Concatenated string with uncommon characters of two strings link. We can solve this problem quickly in Python using Set and List Comprehension. Approach is simple,

  1. Convert both strings into set so that they could have only unique characters. Now take intersection of two sets to get common character both strings have.
  2. Now separate out those characters in each string which are not common in both of them and concatenate the characters.




# Function to concatenated string with uncommon 
# characters of two strings 
def uncommonConcat(str1, str2):
     # convert both strings into set
     set1 = set(str1)
     set2 = set(str2)
     # take intersection of two sets to get list of
     # common characters 
     common = list(set1 & set2)
     # separate out characters in each string
     # which are not common in both strings
     result = [ch for ch in str1 if ch not in common] + \
              [ch for ch in str2 if ch not in common]
     # join each character without space to get 
     # final string
     print ''.join(result)
# Driver program
if __name__ == "__main__":
    str1 = 'aacdb'
    str2 = 'gafd'




Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry Skilled in Big Data technologies, Data Structures, Algorithm, Software Development, App Development, Databases, Python, Java, C, and WordPress

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Code_Mech