Open In App

Python Set | Pairs of complete strings in two sets

Last Updated : 21 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Two strings are said to be complete if on concatenation, they contain all the 26 English alphabets. For example, “abcdefghi” and “jklmnopqrstuvwxyz” are complete as they together have all characters from ‘a’ to ‘z’. We are given two sets of sizes n and m respectively and we need to find the number of pairs that are complete on concatenating each string from set 1 to each string from set 2. Examples:

Input : set1[] = {"abcdefgh", "geeksforgeeks",
                 "lmnopqrst", "abc"}
        set2[] = {"ijklmnopqrstuvwxyz", 
                 "abcdefghijklmnopqrstuvwxyz", 
                 "defghijklmnopqrstuvwxyz"} 
Output : 7
The total complete pairs that are forming are:
"abcdefghijklmnopqrstuvwxyz"
"abcdefghabcdefghijklmnopqrstuvwxyz"
"abcdefghdefghijklmnopqrstuvwxyz"
"geeksforgeeksabcdefghijklmnopqrstuvwxyz"
"lmnopqrstabcdefghijklmnopqrstuvwxyz"
"abcabcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz"

We have existing solution for this problem please refer Pairs of complete strings in two sets of strings link. We can solve this problem quickly in python using Set data structure. Approach is very simple,

  1. Consider all pairs of strings, concatenate them one by one and converts it into set.
  2. Now one by one add all alphabets in concatenated string into set. Since set contains unique values so if length of set is equal to 26 that means set contains all 26 english alphabets.

Python3




# Function to find pairs of complete strings
# in two sets of strings
   
def completePair(set1,set2):
       
    # consider all pairs of string from
    # set1 and set2
    count = 0
    for str1 in set1:
        for str2 in set2:
            result = str1 + str2
   
            # push all alphabets of concatenated 
            # string into temporary set
            tmpSet = set([ch for ch in result if (ord(ch)>=ord('a') and ord(ch)<=ord('z'))])
            if len(tmpSet)==26:
                count = count + 1
    print (count)
   
# Driver program
if __name__ == "__main__":
    set1 = ['abcdefgh', 'geeksforgeeks','lmnopqrst', 'abc']
    set2 = ['ijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz','defghijklmnopqrstuvwxyz']
    completePair(set1,set2)


Output

7

Time complexity: O(n^2 * m). 
Auxiliary space: O(1).



Similar Reads

Pairs of complete strings in two sets of strings
Two strings are said to be complete if on concatenation, they contain all the 26 English alphabets. For example, "abcdefghi" and "jklmnopqrstuvwxyz" are complete as they together have all characters from 'a' to 'z'. We are given two sets of sizes n and m respectively and we need to find the number of pairs that are complete on concatenating each st
15 min read
Python Program to Find Duplicate sets in list of sets
Given a list of sets, the task is to write a Python program to find duplicate sets. Input : test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]Output : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]Explanation : {1, 4, 5, 6} is similar to {6, 4, 1, 5} hence part of result. Input : test_list = [{4, 5, 6, 9}, {6, 4, 1, 5}, {
8 min read
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array
Given an array arr[] consisting of N strings, the task is to find the pair of strings that is not present in the array formed by any pairs (arr[i], arr[j]) by swapping the first characters of the strings arr[i] and arr[j]. Examples: Input: arr[] = {"good", "bad", "food"}Output: 2Explanation:The possible pairs that can be formed by swapping the firs
13 min read
Distribute given arrays into K sets such that total sum of maximum and minimum elements of all sets is maximum
Given two arrays, the first arr[] of size N and the second brr[] of size K. The task is to divide the first array arr[] into K sets such that the i-th set should contain brr[i] elements from the second array brr[], and the total sum of maximum and minimum elements of all sets is maximum. Examples: Input: n = 4, k = 2, arr[] = {10, 10, 11, 11 }, brr
8 min read
Ways to select one or more pairs from two different sets
Given two positive numbers 'n' and 'm' (n &lt;= m) which represent total number of items of first and second type of sets respectively. Find total number of ways to select at-least one pair by picking one item from first type(I) and another item from second type(II). In any arrangement, an item should not be common between any two pairs. Note: Sinc
9 min read
Total character pairs from two strings, with equal number of set bits in their ascii value
Given two strings s1 and s2. The task is to take one character from the first string and one character from the second string and check if the ASCII values of both characters have the same number of set bits. Print the total number of such pairs. Examples: Input: s1 = "xcd", s2 = "swa" Output: 1 Only valid pair is (d, a) with ASCII values as 100 an
6 min read
Equally divide into two sets such that one set has maximum distinct elements
There are two processes P1 and P2, and N resources where N is an even number. There is an array of N size and arr[i] represents the type of ith resource.There may be more than one instance of a resource.You are to divide these resources equally between P1 and P2 such that maximum number of distinct number of resources are allocated to P2. Print max
8 min read
Sets of pairs in C++
Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. Pair is a simple container defined in &lt;utility&gt; header consisting
3 min read
Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
Given an array string[] consisting of N numeric strings of length M, the task is to find the number of distinct strings that can be generated by selecting any two strings, say i and j from the array and swap all possible prefixes between them. Note: Since the answer can be very large, print modulo 1000000007. Examples: Input: N = 2 M = 3 string[] =
6 min read
Join Two Sets In One Line Without Using "|" in Python
In this article, we will understand how to join two sets in one line without using "|".As we know when working with sets in Python, there might be scenarios where you need to combine two sets into a single set. The easiest way to achieve this is by using the union operator (|). However, we need to join two sets in one line without using "|". So in
3 min read
Article Tags :
Practice Tags :