Concatenated string with unusual characters in Python

Python Methods and Functions | String Variables

Examples :

 Input: S1 = "aacdb" S2 = "gafd" Output: "cbgf" Input: S1 = "abcs"; S2 = "cxzca"; Output: "bsxz" 

This problem has an existing solution, please refer to a concatenated string with unusual characters from two lines . We can quickly fix this problem in Python using

# Function for concatenated line with unusual
# two line characters

 

def uncommonConcat (str1, str2) :

 

# convert both lines to a set

  set1 = set (str1)

set2 = set (str2)

 

# take the intersection two sets to get a list

# common characters

common = list (set1 & amp; set2)

 

# highlight characters on each line

# that don't appear on both lines

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 a space to get

# final line

print '' .join (result)

 
# Driver program

if __ name__ = = " __ main__ " :

  str1 = 'aacdb'

str2 = 'gafd'

  uncommonConcat (str1, str2)

Output:

 cbgf 




Tutorials