Open In App

Python Set – Pairs of Complete Strings in Two Sets

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of finding pairs of complete strings in two sets in Python involves identifying string pairs from two different lists such that, when combined, they contain all the letters of the English alphabet. For example, given two sets a = [‘abcdefgh’, ‘geeksforgeeks’, ‘lmnopqrst’, ‘abc’] and b = [‘ijklmnopqrstuvwxyz’, ‘abcdefghijklmnopqrstuvwxyz’, ‘defghijklmnopqrstuvwxyz’], the task is to count how many string pairs one from each set together cover all letters. The output will be 7.

Using bit masking

Bitmasking is an efficient technique to identify pairs of complete strings from two sets using bitwise operations. It checks if the combined characters from two strings cover all 26 letters of the alphabet.

Example:

Python
ALL_LETTERS = (1 << 26) - 1  # all letters in bit format

# Initializes counter c
c = 0  
a = ['abcdefgh', 'geeksforgeeks', 'lmnopqrst', 'abc']
b = ['ijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz', 'defghijklmnopqrstuvwxyz']

for s1 in a:
    for s2 in b:
        # Get bitmask representation of s1
        mask1 = 0
        for char in s1:
            mask1 |= (1 << (ord(char) - ord('a')))
        
        # Get bitmask representation of s2
        mask2 = 0
        for char in s2:
            mask2 |= (1 << (ord(char) - ord('a')))
        res = mask1 | mask2
        if res == ALL_LETTERS:
            c += 1
print(c)

Output
7

Explanation:

  • for s1 in a, for s2 in b: This iterates through all pairs of strings from lists a and b.
  • mask1 = 0, mask2 = 0: This initializes bitmasks for strings s1 and s2.
  • for char in s1, for char in s2: This loops through characters of s1 and s2.
  • mask1 |= (1 << (ord(char) – ord(‘a’))), mask2 |= (1 << (ord(char) – ord(‘a’))): This sets corresponding bits in mask1 and mask2 for each character.
  • res = mask1 | mask2: This combines the bitmasks of s1 and s2 to form res.
  • if res == ALL_LETTERS: This checks if the combined bitmask res represents all 26 letters from ‘a’ to ‘z’, and if true, it increments the counter c by 1 to count the valid pair of strings, and finally, the total count c is printed.

Let’s understand different methods to find Pairs of complete strings in two sets.

Using set

This method combines two strings, turns the combined string into a set which removes duplicates and then checks if the set contains all 26 letters of the alphabet.

Example:

Python
a = ['abcdefgh', 'geeksforgeeks', 'lmnopqrst', 'abc']
b = ['ijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz', 'defghijklmnopqrstuvwxyz']

c = 0 
for i in a:
    for j in b:
        combine = i + j
        if len(set(combine)) == 26:
            c += 1
print(c)

Output
7

Explanation:

  • for i in a, for j in b loops through all pairs of strings i from a and j from b.
  • i + j concatenate strings i and j.
  • if len(set(combine)) == 26 check if combined string contains all 26 letters.
  • c += 1 increment count when condition is met.

Using counter

Counter method counts how many times each character appears in the combined string and checks if every letter from the alphabet is present at least once.

Example:

Python
from collections import Counter

a = ['abcdefgh', 'geeksforgeeks', 'lmnopqrst', 'abc']
b = ['ijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz', 'defghijklmnopqrstuvwxyz']

c = 0  # initialize count
for i in a:
    for j in b:
        combine = i + j
        char_count = Counter(combine)
        if len(char_count) == 26 and all(char_count[chr(k + ord('a'))] > 0 for k in range(26)):
            c += 1
print(c)

Output
7

Explanation:

  • char_count = Counter(combine) counts the frequency of characters in the combined string.
  • len(char_count) == 26 ensures the combined string has 26 unique characters.
  • all(char_count[chr(k + ord('a'))] > 0 for k in range(26)) checks that every letter from ‘a’ to ‘z’ appears at least once in the combined string.


Next Article
Article Tags :
Practice Tags :

Similar Reads