
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if All Strings are Mutually Disjoint in Python
String is one of the data structures which hold the data enclosed in double quotes or in single quotes i.e. any data type enclosed inside the quotes considered as string. It is immutable, once we define the data we cannot change or modify. In python we have a function called str() which takes any data as the input and returns the string as output.
Mutually disjoint means if no two strings are having the same elements in common. There are different ways to check if all strings are mutually disjoint. Let's go through each one.
Using nested loops
Nested loop is the approach related to loops which creates another loop within the main loop.
Example
In this example we are creating nested loop for checking each and every element in both the string, if the strings are mutually disjoint returns output as True or returns output as False. The below is the example of checking the strings whether they are mutually disjoint or not.
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.")
Output
Strings are not mutually disjoint.
Using the any() function
In python we have the function known as any() which returns True if any element in the iterable is True otherwise returns False i.e. if any element is as same as the element in the other string then it returns True otherwise returns False.
Example
In this example we are passing the strings as the input parameter to the any() function and checks if any of the strings are similar, then returns output as True or False.
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 = ["hello", "world", "python"] if are_disjoint(my_strings): print("All strings are mutually disjoint.") else: print("Strings are not mutually disjoint.")
Output
Strings are not mutually disjoint.
Using the all() function
The other function available in python is all() function, which returns True if all the iterables are True otherwise returns False.
Example
In this example we will iterate through the strings and check for any common characters between each pair of strings. If a common character is found, the functions return False, indicating that the strings are not mutually disjoint. If no common characters are found, the functions return True.
def are_disjoint(strings): return all(not set(strings[i]).intersection(*map(set, strings[:i] + strings[i+1:])) for i in range(len(strings))) my_strings = ["hello", "world", "python"] if are_disjoint(my_strings): print("All strings are mutually disjoint.") else: print("Strings are not mutually disjoint.")
Output
Strings are not mutually disjoint.