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
# Python 3 code to demonstrate # Union Operation in two Strings # using naive method # initializing strings test_str1 = 'Lazyroar' test_str2 = 'Codefreaks' # Printing initial strings print ( "The original string 1 is : " + test_str1) print ( "The original string 2 is : " + test_str2) # using naive method to # Union Operation in two Strings res = "" temp = test_str1 for i in test_str2: if i not in temp: test_str1 + = i # printing result print ( "The string union is : " + test_str1) |
The original string 1 is : Lazyroar The original string 2 is : Codefreaks The string union is : LazyroarCda
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
# Python 3 code to demonstrate # Union Operation in two Strings # using set() + union() # initializing strings test_str1 = 'Lazyroar' test_str2 = 'Codefreaks' # Printing initial strings print ("The original string 1 is : " + test_str1) print ("The original string 2 is : " + test_str2) # using set() + union() to # Union Operation in two Strings res = set (test_str1).union(test_str2) # printing result print ("The string union is : " + str (res)) |
The original string 1 is : Lazyroar 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
# Python 3 code to demonstrate # Union Operation in two Strings # using | operator # initializing strings test_str1 = 'Lazyroar' test_str2 = 'Codefreaks' # Printing initial strings print ( "The original string 1 is : " + test_str1) print ( "The original string 2 is : " + test_str2) # using | operator to perform union res = set (test_str1) | set (test_str2) # printing result print ( "The string union is : " ,res) |
The original string 1 is : Lazyroar 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
# Python3 code to demonstrate # Union Operation in two Strings # using reduce from functools import reduce # Initializing strings test_str1 = 'Lazyroar' test_str2 = 'Codefreaks' # Printing initial strings print ( "The original string 1 is : " + test_str1) print ( "The original string 2 is : " + test_str2) # Using reduce to perform Union Operation on two strings res = reduce ( lambda acc, c: acc + c if c not in acc else acc, test_str2, test_str1) # Printing the result print ( "The string union is : " + res) |
The original string 1 is : Lazyroar The original string 2 is : Codefreaks The string union is : LazyroarCda
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”.