Python - Filter Supersequence Strings
Last Updated :
27 Apr, 2023
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", "/", "geeksforgeeks", "best", "for", "geeks"], substr = "kgs"
Output : ["geeksforgeeks", "geeks"]
Explanation : All kgs characters are present in both strings.
Input : test_list = ["gfg", "/", "geeksforgeeks", "best", "for", "geeks"], substr = "kgf"
Output : ["geeksforgeeks"]
Explanation : All kgs characters are present in only geeksforgeeks 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", "/", "geeksforgeeks", "best", "for", "geeks"]
# 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))
OutputThe original list is : ['gfg', '/', 'geeksforgeeks', 'best', 'for', 'geeks']
Filtered strings : ['geeksforgeeks', 'geeks']
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", "/", "geeksforgeeks", "best", "for", "geeks"]
# 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))
OutputThe original list is : ['gfg', '/', 'geeksforgeeks', 'best', 'for', 'geeks']
Filtered strings : ['geeksforgeeks', 'geeks']
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", "/", "geeksforgeeks", "best", "for", "geeks"]
# 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))
OutputThe original list is : ['gfg', '/', 'geeksforgeeks', 'best', 'for', 'geeks']
Filtered strings : ['geeksforgeeks', 'geeks']
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", "/", "geeksforgeeks", "best", "for", "geeks"]
# 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))
OutputFiltered strings : ['geeksforgeeks', 'geeks']
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.
Similar Reads
Python - Test substring order Given two strings, check if substring characters occur in correct order in string. Input : test_str = 'geeksforgeeks', K = 'sees' Output : True Explanation : "s" after that "ee" and then "s" is present in order in string 1. Input : test_str = 'geeksforgeeks', K = 'seef' Output : False Explanation :
4 min read
Python - Filter Strings within ASCII range Given ASCII or alphabetical range, filter strings are found in a particular range. Input : test_list = ["gfg", "is", "best", "for", "geeks"], strt_asc, end_asc = 105, 115 Output : ['is'] Explanation : i has 105, and s has 115, which is in range ASCII values.Input : test_list = ["gfg", "is", "best",
3 min read
Test if String is Subset of Another - Python We are given two strings s1 and s2 and the task is to check whether s2 is a subsequence of s1. A subsequence is a sequence that appears in the same relative order but not necessarily consecutively. Using all() In this method we check whether all the characters of s2 are present in s1 by using the al
3 min read
Test if String is Subset of Another - Python We are given two strings s1 and s2 and the task is to check whether s2 is a subsequence of s1. A subsequence is a sequence that appears in the same relative order but not necessarily consecutively. Using all() In this method we check whether all the characters of s2 are present in s1 by using the al
3 min read
Test if String is Subset of Another - Python We are given two strings s1 and s2 and the task is to check whether s2 is a subsequence of s1. A subsequence is a sequence that appears in the same relative order but not necessarily consecutively. Using all() In this method we check whether all the characters of s2 are present in s1 by using the al
3 min read
Python - Filter list of strings based on the substring list The problem requires to check which strings in the main list contain any of the substrings from a given list and keep only those that match. Let us explore this problem and understand different methods to solve it.Using list comprehension with any() (Most Efficient)List comprehension is a concise an
4 min read