Concatenated string with uncommon characters in Python
Last Updated :
12 Apr, 2023
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.
Examples:
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.
Implementation:
Python3
def uncommonConcat(str1, str2):
set1 = set (str1)
set2 = set (str2)
common = list (set1 & set2)
result = [ch for ch in str1 if ch not in common] + [ch for ch in str2 if ch not in common]
print ( ''.join(result) )
if __name__ = = "__main__" :
str1 = 'aacdb'
str2 = 'gafd'
uncommonConcat(str1,str2)
|
Time Complexity: O(n+m), where n is length of str1 and m is length of str2
Auxiliary Space: O(m), where m is length of result list.
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.
Implementation:
Python3
def uncommonConcat(str1, str2):
set1 = set (str1)
set2 = set (str2)
uncommon = list (set1 ^ set2)
print ( ''.join(uncommon) )
if __name__ = = "__main__" :
str1 = 'aacdb'
str2 = 'gafd'
uncommonConcat(str1,str2)
|
Approach #3: Using Counter() function
Python3
from collections import Counter
def uncommonConcat(str1, str2):
result = []
frequency_str1 = Counter(str1)
frequency_str2 = Counter(str2)
for key in frequency_str1:
if key not in frequency_str2:
result.append(key)
for key in frequency_str2:
if key not in frequency_str1:
result.append(key)
result.sort()
print (''.join( set (result)))
if __name__ = = "__main__" :
str1 = 'aacdb'
str2 = 'gafd'
uncommonConcat(str1, str2)
|
Approach #4 : Using two loops and no built-in module
In this approach we will use two loops and a variable to store the final result. We will iterate over the each string once and check that certain element is present in the other string or not, if not then we will just concatenate that character with our variable.
Python3
def concatenate_uncommon(str1,str2):
final_str = ''
for i in str1:
if i in str2:
pass
else :
final_str + = i
for j in str2:
if j in str1:
pass
else :
final_str + = j
return final_str
str1 = 'abcs'
str2 = 'cxzca'
print (concatenate_uncommon(str1,str2))
str1 = 'aacdb'
str2 = 'gafd'
print (concatenate_uncommon(str1,str2))
|
Time Complexity - O(n+m) # n = length of string 1 and m = length of string 2.
Auxiliary Space - O(1) # only one extra variable has been used
Please Login to comment...