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”.
Similar Reads
Convert tuple to string in Python
The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore
3 min read
Convert String to Tuple - Python
When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte
2 min read
Python program to find uncommon words from two Strings
The task of finding uncommon words from two strings in Python involves identifying words that appear in only one of the given strings while ignoring common words. Given two input strings, the goal is to extract words that are unique to each string. For example, with A = "Geeks for Geeks" and B = "Le
3 min read
Python - Convert String to unicode characters
Convert String to Unicode characters means transforming a string into its corresponding Unicode representations. Unicode is a standard for encoding characters, assigning a unique code point to every character. For example: string "A" has the Unicode code point U+0041.string "ä½ å¥½" corresponds to U+4F6
2 min read
Python - Concatenation of two String Tuples
Sometimes, while working with records, we can have a problem in which we may need to perform String concatenation of tuples. This problem can occur in day-day programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression The combination o
3 min read
Python | Add one string to another
The concatenation of two strings has been discussed multiple times in various languages. But the task is how to add to a string in Python or append one string to another in Python. Example Input: 'GFG' + 'is best' Output: 'GFG is best' Explanation: Here we can add two string using "+" operator in Py
5 min read
Python Program to Convert a List to String
In Python, converting a list to a string is a common operation. In this article, we will explore the several methods to convert a list into a string. The most common method to convert a list of strings into a single string is by using join() method. Let's take an example about how to do it. Using th
3 min read
Python - Words Lengths in String
We are given a string we need to find length of each word in a given string. For example, we are s = "Hello world this is Python" we need to find length of each word so that output should be a list containing length of each words in sentence, so output in this case will be [5, 5, 4, 2, 6]. Using Lis
2 min read
Convert String List to ASCII Values - Python
We need to convert each character into its corresponding ASCII value. For example, consider the list ["Hi", "Bye"]. We want to convert it into [[72, 105], [66, 121, 101]], where each character is replaced by its ASCII value. Let's discuss multiple ways to achieve this. Using List Comprehension with
3 min read
Find the Union on List of Sets in Python
The union of sets involves combining distinct elements from multiple sets into a single set, eliminating duplicates. In this article, we will study how to find the union on a list of sets in Python. Find the Union on a List of Sets in PythonBelow are some of the ways by which we can find the union o
4 min read