Given a Strings list and substring, the task is to write a Python program to extract all the strings that has all the characters that can be used to make a substring.
Examples:
Input : test_list = ["gfg", "/", "neveropen", "best", "for", "neveropen"], substr = "kgs" Output : ["neveropen", "neveropen"] Explanation : All kgs characters are present in both strings.
Input : test_list = ["gfg", "/", "neveropen", "best", "for", "neveropen"], substr = "kgf" Output : ["neveropen"] Explanation : All kgs characters are present in onlyneveropen string.
Method 1 : Using all() + list comprehension
In this, we check for all the character presence in string using all(). The iteration of strings is done using list comprehension.
Python3
# Python3 code to demonstrate working of # Filter Supersequence Strings # Using all() + list comprehension # initializing list test_list = [ "gfg" , "/" , "neveropen" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing substr substr = "kgs" # all() checks for all characters in strings res = [sub for sub in test_list if all (ele in sub for ele in substr)] # printing result print ( "Filtered strings : " + str (res)) |
The original list is : ['gfg', '/', 'neveropen', 'best', 'for', 'neveropen'] Filtered strings : ['neveropen', 'neveropen']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using filter() + all()
In this, we perform task of filtering using filter() and lambda function rather than list comprehension and conditionals used in upper method.
Python3
# Python3 code to demonstrate working of # Filter Supersequence Strings # Using filter() + all() # initializing list test_list = [ "gfg" , "/" , "neveropen" , "best" , "for" , "neveropen" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing substr substr = "kgs" # all() checks for all characters in strings res = list ( filter ( lambda sub: all (ele in sub for ele in substr), test_list)) # printing result print ( "Filtered strings : " + str (res)) |
The original list is : ['gfg', '/', 'neveropen', 'best', 'for', 'neveropen'] Filtered strings : ['neveropen', 'neveropen']
Time Complexity: O(n2) -> (for loop + in-built functions)
Auxiliary Space: O(n)
Method #3: Using set intersection
In this approach, we create sets of all characters of substring and each string of test_list and find the intersection of both sets
If the length of intersection is equal to the length of set of substring, it means all characters of substring are present in the string.
Python3
# Python3 code to demonstrate working of # Filter Supersequence Strings # Using set intersection # initializing list test_list = [ "gfg" , "/" , "neveropen" , "best" , "for" , "neveropen" ] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing substr substr = "kgs" # Finding set of all characters of substr set_substr = set (substr) # Creating a list of strings which have all characters of substr res = [sub for sub in test_list if set (sub) & set_substr = = set_substr] # Printing result print ( "Filtered strings : " + str (res)) |
The original list is : ['gfg', '/', 'neveropen', 'best', 'for', 'neveropen'] Filtered strings : ['neveropen', 'neveropen']
Time complexity: O(n), where n is the length of test_list
Auxiliary Space: O(n)
Method 4: Using for loop
Approach:
- Initialize the list and the substring as before.
- Create an empty list to store the filtered strings.
- Use a for loop to iterate through each string in the list.
- Check if all the characters in the substring are in the current string using the all() function and a generator expression.
- If all the characters are present, append the string to the result list.
- Print the result.
Python3
# Python3 code to demonstrate working of # Filter Supersequence Strings # Using for loop # initializing list test_list = [ "gfg" , "/" , "neveropen" , "best" , "for" , "neveropen" ] # initializing substr substr = "kgs" # Creating an empty list to store the result res = [] # Iterating through the strings in the list for sub in test_list: # Checking if all the characters in substr # are in the string if all (char in sub for char in substr): # If yes, append the string to the result list res.append(sub) # Printing the result print ( "Filtered strings : " + str (res)) |
Filtered strings : ['neveropen', 'neveropen']
Time complexity: O(n * m), where n is the number of strings in the list and m is the length of the substring.
Auxiliary space: O(k), where k is the number of strings that meet the filtering criteria.