Open In App

Python – Disjoint Strings across Lists

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two lists, extract all the string pairs which are disjoint across i.e. which don’t have any character in common.

Input : test_list1 = [“haritha”, “is”, “best”], test_list2 = [“she”, “loves”, “papaya”] 
Output : [(‘haritha’, ‘loves’), (‘is’, ‘papaya’), (‘best’, ‘papaya’)] 
Explanation : “is” and “papaya” has no character in common.

Input : test_list1 = [aa, cab], test_list2 = [“a”, “c”] 
Output : [] 
Explanation : No pair of disjoint Strings. 

Approach: Using set() + yield [ generator ] + reduce() + recursion

In this, we perform tasks of getting the disjoint strings using set & operation and extract dynamically using yield. Each subsequent string is checked for disjoint using recursion.

Python3




# Python3 code to demonstrate working of
# Disjoint Strings across Lists
# Using set() + yield [ generator ] + reduce() + recursion
from functools import reduce
 
# helper function
def dis_pairs(dpair, res=[]):
 
    # checking for disjoint pair
    if not dpair and not reduce(lambda a, b: set(a) & set(b), res):
        yield tuple(res)
 
    # recurring for subsequent pairs
    elif dpair:
        yield from [idx for k in dpair[0] for idx in dis_pairs(dpair[1:], res + [k])]
 
 
# initializing lists
test_list1 = ["haritha", "is", "best"]
test_list2 = ["she", "loves", "papaya"]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# calling function
res = list(dis_pairs([test_list1, test_list2]))
 
# printing result
print("All possible Disjoint pairs : " + str(res))


Output

The original list 1 is : ['haritha', 'is', 'best']
The original list 2 is : ['she', 'loves', 'papaya']
All possible Disjoint pairs : [('haritha', 'loves'), ('is', 'papaya'), ('best', 'papaya')]

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

Another approach – Using set() + list comprehension
This approach involves iterating through each word of both the lists and checking if there are no common characters using set(). If there are no common characters, the word pairs are appended to a result list. Finally, the result list is returned.

Python3




def disjoint_strings(l1, l2):
    result = [(w1, w2) for w1 in l1 for w2 in l2 if not set(w1) & set(w2)]
    return result
 
# test
test_list1 = ["haritha", "is", "best"]
test_list2 = ["she", "loves", "papaya"]
print(disjoint_strings(test_list1, test_list2))


Output

[('haritha', 'loves'), ('is', 'papaya'), ('best', 'papaya')]

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

Method 3 :  use nested loops with the combination function from itertools module.

step-by-step approach:

  1. Import the itertools module to use the combination function.
  2. Define the function disjoint_pairs that takes two lists as input.
  3. Use the combination function to generate all possible pairs of elements from the two lists.
  4. Iterate over each pair, check if they are disjoint, and append them to a result list if they are.
  5. Return the result list.

Python3




import itertools
 
def disjoint_pairs(list1, list2):
    result = []
    for pair in itertools.product(list1, list2):
        if set(pair[0]).isdisjoint(pair[1]):
            result.append(pair)
    return result
 
# initializing lists
test_list1 = ["haritha", "is", "best"]
test_list2 = ["she", "loves", "papaya"]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# calling function
res = disjoint_pairs(test_list1, test_list2)
 
# printing result
print("All possible Disjoint pairs : " + str(res))


Output

The original list 1 is : ['haritha', 'is', 'best']
The original list 2 is : ['she', 'loves', 'papaya']
All possible Disjoint pairs : [('haritha', 'loves'), ('is', 'papaya'), ('best', 'papaya')]

Time complexity: O(n^2) where n is the length of the input lists because we generate all possible pairs of elements from both lists.

Auxiliary space: O(m) where m is the number of disjoint pairs found because we store the pairs in the result list.

METHOD 4:Using lambda method

APPROACH:

This program finds all possible disjoint pairs of strings from two given lists using the lambda function in Python

ALGORITHM:

1.Create two input lists of strings.
2.Using a lambda function and filter(), iterate through all possible pairs of the two lists.
3.Check if the set of characters in the first string is disjoint from the set of characters in the second string.
4.If the sets are disjoint, add the pair to a new list.
5.Print the list of disjoint pairs

Python3




list1 = ['haritha', 'is', 'best']
list2 = ['she', 'loves', 'papaya']
 
disjoint_pairs = list(filter(lambda x: set(x[0]).isdisjoint(set(x[1])), [(x, y) for x in list1 for y in list2]))
print("All possible Disjoint pairs:", disjoint_pairs)


Output

All possible Disjoint pairs: [('haritha', 'loves'), ('is', 'papaya'), ('best', 'papaya')]

Time complexity: O(n^2)
Space complexity: O(n^2)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads