Concatenated string with uncommon characters in Python
Two strings are given and you have to modify the 1st string such that all the common characters of the 2nd string have to be removed and the uncommon characters of the 2nd string have to be concatenated with the uncommon characters of the 1st string.
Input : S1 = “aacdb”, S2 = “gafd”
Output : “cbgf”
Input : S1 = “abcs”;, S2 = “cxzca”;
Output : “bsxz”
This problem has an 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. The approach is simple,
- Convert both strings into a set so that they could have only unique characters. Now take the intersection of two sets to get the common character both strings have.
- Now separate out those characters in each string that are not common in both of them and concatenate the characters.
Approach#2: Using set symmetric difference
We can use the symmetric difference operation of set to pull out all the uncommon character from both the string and make a string.
Approach #3: Using Counter() function
Please Login to comment...