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:
- 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 s
2
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)
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)
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.
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 o
15 min read
Convert Set to String in Python Converting a set to a string in Python means changing a group of unique items into a text format that can be easily read and used. Since sets do not have a fixed order, the output may look different each time. For example, a set {1, 2, 3} can be turned into the string "{1, 2, 3}" or into "{3, 1, 2}"
2 min read
Convert String to Set in Python There are multiple ways of converting a String to a Set in python, here are some of the methods.Using set()The easiest way of converting a string to a set is by using the set() function.Example 1 : Pythons = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s) print(type(set_s)) pr
1 min read
Intersection of two String - Python We are given two strings and our task is to find the intersection of characters between them. This means we need to identify which characters are common in both strings, regardless of their position. For example, if we have strings like "GeeksforGeeks" and "Codefreaks", the common characters would b
3 min read
How to Concatenate Two Lists Index Wise in Python Concatenating two lists index-wise means combining corresponding elements from both lists into a single element, typically a string, at each index . For example, given two lists like a = ["gf", "i", "be"] and b = ["g", "s", "st"], the task is to concatenate each element from a with the corresponding
3 min read