Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Intersection of two String

Python | Intersection of two String

One of the string operations can be computing the intersection of two strings i.e, output the common values that appear in both the strings. There are various ways in Python, through which we can perform the Intersection of two strings. Method #1 : Naive Method Create an empty string and check for new occurrence of character common to both string and appending it. Hence computing the new intersection string. This can be achieved by loops and if/else statements. 

Python3




# Python3 code to demonstrate
# string intersection
# using naive method
 
# initializing strings
test_str1 = 'Lazyroar'
test_str2 = 'Codefreaks'
 
# using naive method to
# get string intersection
res = ""
for i in test_str1:
    if i in test_str2 and not i in res:
        res += i
         
# printing intersection
print ("String intersection is : " + res)


Output :

String intersection is : eksfor

Time Complexity: O(n2)
Auxiliary Space: O(n)

  Method #2 : Using set() + intersection() Firstly, both the strings are converted into sets using set() and then intersection is performed using intersection(). Returns the sorted set. 

Python3




# Python3 code to demonstrate
# string intersection
# using set() + intersection()
 
# initializing strings
test_str1 = 'Lazyroar'
test_str2 = 'Codefreaks'
 
# using set() + intersection() to
# get string intersection
res = set(test_str1).intersection(test_str2)
         
# printing intersection
print ("String intersection is : " + str(res))


Output :

String intersection is : {'e', 'f', 's', 'o', 'k', 'r'}

Time Complexity: O(n)
Auxiliary Space: O(n)

  Method #3 : Using join() join() performs the task similar to list comprehension in case of lists. This encapsulates whole intersection logic and joins together each element filtered through the intersection logic into one string, hence computing the intersection. It converts the strings into set and then computed & operation on them. 

Python3




# Python3 code to demonstrate
# string intersection
# using join()
 
# initializing strings
test_str1 = 'Lazyroar'
test_str2 = 'Codefreaks'
 
# using join() to
# get string intersection
res = ''.join(sorted(set(test_str1) &
         set(test_str2), key = test_str1.index))
         
# printing intersection
print ("String intersection is : " + str(res))


Output :

String intersection is : eksfor

Time Complexity: O(n.log n)
Auxiliary Space: O(n)

 Method #4 : using filter and lambda function

Python3




test_str1 = 'Lazyroar'
test_str2 = 'Codefreaks'
 
# using lambda and list comprehension
res = "".join(filter(lambda i: i in test_str2, test_str1))
res = {i for i in res}
# printing intersection
print("String intersection is : " + str(res))
#This code is contributed by Jyothi Pinjala.


Output

String intersection is : {'r', 'f', 'k', 'e', 'o', 's'}

Time Complexity: O(n)
Auxiliary Space: O(n)

Approach#5:using & operator

Algorithm

1.Define a function named string_intersection that takes two strings s1 and s2 as input.
2.Create a set of unique characters from s1 using the set() function.
3.Create another set of unique characters from s2 using the set() function.
4.Find the intersection of these two sets using the & operator, which returns a new set that contains only the elements that are common to both sets.
5.Convert this set into a sorted list using the sorted() function.
6.Join the elements of the sorted list into a single string using the join() function.
7.Return the resulting string as the output of the function.

Python3




def string_intersection(s1, s2):
    intersection = set(s1) & set(s2)
    return ''.join(sorted(intersection))
s1= 'Lazyroar'
s2 = 'Codefreaks'
print(string_intersection(s1, s2))


Output

efkors

Time complexity: O(n log n) – due to the sorted() function
Space complexity: O(n) – where n is the size of the intersection set.

RELATED ARTICLES

Most Popular

Recent Comments