A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-set-pairs-complete-strings-two-sets/ below:

Python Set - Pairs of Complete Strings in Two Sets

Python Set - Pairs of Complete Strings in Two Sets

Last Updated : 04 Feb, 2025

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)

Explanation:

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)

Explanation:

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)

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4