Python | Union Operation in two Strings
Last Updated :
16 May, 2023
One of the string operation can be computing the union of two strings. This can be useful application that can be dealt with. This article deals with computing the same through different ways.
Method 1 : Naive Method The task of performing string union can be computed by naive method by creating an empty string and checking for new occurrence of character common to both string and not common strings and appending it and hence computing the new union string. This can be achieved by loops and if/else statements.
Python3
test_str1 = 'GeeksforGeeks'
test_str2 = 'Codefreaks'
print ( "The original string 1 is : " + test_str1)
print ( "The original string 2 is : " + test_str2)
res = ""
temp = test_str1
for i in test_str2:
if i not in temp:
test_str1 + = i
print ( "The string union is : " + test_str1)
|
Output :
The original string 1 is : GeeksforGeeks
The original string 2 is : Codefreaks
The string union is : GeeksforGeeksCda
Time Complexity: O(n*n), where n is the number of elements in the “test_str”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_str”.
Method 2 : Using set() + union() Set in python usually can perform the task of performing set operations such as set union. This utility of sets can be used to perform this task as well. Firstly, both the strings are converted into sets using set() and then union is performed using union(). Returns the sorted set.
Python3
test_str1 = 'GeeksforGeeks'
test_str2 = 'Codefreaks'
print ("The original string 1 is : " + test_str1)
print ("The original string 2 is : " + test_str2)
res = set (test_str1).union(test_str2)
print ("The string union is : " + str (res))
|
Output :
The original string 1 is : GeeksforGeeks
The original string 2 is : Codefreaks
The string union is : {'s', 'G', 'r', 'e', 'o', 'f', 'k', 'C', 'd', 'a'}
Method 3 : Using set() + | Another approach to perform the union operation on two strings could be using the | operator. The | operator returns a set that contains all elements from the first set and all elements from the second set that are not present in the first set.
Here is an example implementation:
Python3
test_str1 = 'GeeksforGeeks'
test_str2 = 'Codefreaks'
print ( "The original string 1 is : " + test_str1)
print ( "The original string 2 is : " + test_str2)
res = set (test_str1) | set (test_str2)
print ( "The string union is : " ,res)
|
Output
The original string 1 is : GeeksforGeeks
The original string 2 is : Codefreaks
The string union is : {'r', 'a', 'k', 'o', 's', 'G', 'd', 'e', 'C', 'f'}
The time complexity of this approach would be O(len(test_str1) + len(test_str2)) since we need to create sets from both strings and then perform the union operation on them. The space complexity would be O(len(res)) as the size of the result set would be equal to the number of unique characters in the union of the two strings.
Method 4 : Using reduce
In this method we first import the reduce function from functools. Then, we initialize two strings test_str1 and test_str2. We print the initial strings and then use reduce to perform the union operation. In the lambda function, we check if the character c is already present in the accumulated string acc. If it is not present, we append it to the accumulated string, otherwise we just return the accumulated string. We provide the initial accumulated string as test_str1 and the iterable as test_str2. Finally, we print the result of the union operation.
Python3
from functools import reduce
test_str1 = 'GeeksforGeeks'
test_str2 = 'Codefreaks'
print ( "The original string 1 is : " + test_str1)
print ( "The original string 2 is : " + test_str2)
res = reduce ( lambda acc, c: acc + c if c not in acc else acc, test_str2, test_str1)
print ( "The string union is : " + res)
|
Output
The original string 1 is : GeeksforGeeks
The original string 2 is : Codefreaks
The string union is : GeeksforGeeksCda
Time Complexity: O(n*n), where n is the length of the concatenated string.
Auxiliary Space: O(n), where n is the number of elements in the “test_str”.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...