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 maskingBitmasking 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:
a
and b
.s1
and s2
.s1
and s2
.mask1
and mask2
for each character.s1
and s
2
to form res
.Let's understand different methods to find Pairs of complete strings in two sets.
Using setThis 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.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:
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.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