Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to Check all strings are mutually disjoint
String is one of the data structures which holds data enclosed in double quotes or single quotes. It is immutable, meaning once we define the data we cannot change or modify it. In Python we have a function called str() which takes any data as input and returns the string as output.
Mutually disjoint means no two strings have any common characters. There are different ways to check if all strings are mutually disjoint. Let's explore each approach.
Using Nested Loops
This approach uses nested loops to compare each pair of strings by converting them to sets and checking for intersections ?
def are_disjoint(strings):
for i in range(len(strings)):
for j in range(i + 1, len(strings)):
if set(strings[i]) & set(strings[j]):
return False
return True
my_strings = ["Welcome", "to tutorialspoint", "Happy", "Learning"]
if are_disjoint(my_strings):
print("All strings are mutually disjoint.")
else:
print("Strings are not mutually disjoint.")
Strings are not mutually disjoint.
How It Works
The function converts each string to a set of characters and uses the & operator to find common elements. If any intersection exists, it returns False.
Using the any() Function
The any() function returns True if any element in an iterable is True. We can use it with generator expressions to check for common characters ?
def are_disjoint(strings):
return not any(set(strings[i]) & set(strings[j])
for i in range(len(strings))
for j in range(i + 1, len(strings)))
my_strings = ["abc", "def", "ghi"]
if are_disjoint(my_strings):
print("All strings are mutually disjoint.")
else:
print("Strings are not mutually disjoint.")
All strings are mutually disjoint.
Using the all() Function
The all() function returns True if all elements in an iterable are True. This approach checks that each string has no intersection with any other string ?
def are_disjoint(strings):
return all(not set(strings[i]).intersection(set(strings[j]))
for i in range(len(strings))
for j in range(i + 1, len(strings)))
my_strings = ["xyz", "abc", "def"]
if are_disjoint(my_strings):
print("All strings are mutually disjoint.")
else:
print("Strings are not mutually disjoint.")
All strings are mutually disjoint.
Using Set Union for Efficiency
A more efficient approach combines all characters from all strings and compares the total length ?
def are_disjoint(strings):
all_chars = set()
total_unique_chars = 0
for string in strings:
string_set = set(string)
total_unique_chars += len(string_set)
all_chars.update(string_set)
return len(all_chars) == total_unique_chars
# Test with disjoint strings
disjoint_strings = ["abc", "def", "ghi"]
print(f"Test 1: {are_disjoint(disjoint_strings)}")
# Test with overlapping strings
overlapping_strings = ["hello", "world", "python"]
print(f"Test 2: {are_disjoint(overlapping_strings)}")
Test 1: True Test 2: False
Comparison
| Method | Time Complexity | Readability | Best For |
|---|---|---|---|
| Nested Loops | O(n² × m) | High | Small datasets |
| any() Function | O(n² × m) | Medium | Functional programming style |
| Set Union | O(n × m) | Medium | Large datasets |
Conclusion
Use the set union method for better performance with large datasets. The nested loop approach offers the best readability for beginners. All methods effectively check if strings share no common characters.
