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
test_list = [ "gfg" , "/" , "geeksforgeeks" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
substr = "kgs"
res = [sub for sub in test_list if all (ele in sub for ele in substr)]
print ( "Filtered strings : " + str (res))
|
Output
The 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
test_list = [ "gfg" , "/" , "geeksforgeeks" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
substr = "kgs"
res = list ( filter ( lambda sub: all (ele in sub for ele in substr), test_list))
print ( "Filtered strings : " + str (res))
|
Output
The 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
test_list = [ "gfg" , "/" , "geeksforgeeks" , "best" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
substr = "kgs"
set_substr = set (substr)
res = [sub for sub in test_list if set (sub) & set_substr = = set_substr]
print ( "Filtered strings : " + str (res))
|
Output
The 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
test_list = [ "gfg" , "/" , "geeksforgeeks" , "best" , "for" , "geeks" ]
substr = "kgs"
res = []
for sub in test_list:
if all (char in sub for char in substr):
res.append(sub)
print ( "Filtered strings : " + str (res))
|
Output
Filtered 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 - Uppercase Half String
The problem is to convert half of a string to uppercase, either the first half or the second half, depending on the requirement. For example, given the string "python", the output could be "PYThon" (uppercase first half) or "pytHON" (uppercase second half). If the string length is odd, handle the mi
2 min read
Python - Filter Range Length Tuples
We are given a list of tuples where each tuple contains multiple elements, and our task is to filter out tuples whose lengths do not fall within a specified range. For example, if we have a = [(1, 2), (3, 4, 5), (6,), (7, 8, 9, 10)] and the range (2, 3), we should keep only tuples with 2 or 3 elemen
3 min read
Python - Substring presence in Strings List
Given list of substrings and list of string, check for each substring, if they are present in any of strings in List. Input : test_list1 = ["Gfg", "is", "best"], test_list2 = ["I love Gfg", "Its Best for Geeks", "Gfg means CS"] Output : [True, False, False] Explanation : Only Gfg is present as subst
5 min read
Python - Filter above Threshold size Strings
Sometimes, while working with huge amounts of data, we can have a problem in which we need to extract just specific-sized strings above a minimum threshold. This kind of problem can occur during validation cases across many domains. Letâs discuss certain ways to handle this in Python strings list. M
5 min read
Python - Remove after substring in String
Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin
3 min read
Python - Filter Strings combination of K substrings
Given a Strings list, extract all the strings that are a combination of K substrings. Input : test_list = ["geeks4u", "allbest", "abcdef"], substr_list = ["s4u", "est", "al", "ge", "ek", "def"], K = 3 Output : ['geeks4u'] Explanation : geeks4u made up of 3 substr - ge, ek and s4u. Input : test_list
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
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 :
5 min read
Python - Filter rows without Space Strings
Given Matrix, extract rows in which Strings don't have spaces. Examples: Input: test_list = [["gfg is", "best"], ["gfg", "good"], ["gfg is cool"], ["love", "gfg"]] Output: [['gfg', 'good'], ['love', 'gfg']] Explanation: Both the lists have strings that don't have spaces. Input: test_list = [["gfg is
6 min read
Python - Filter float strings from String list
Sometimes, while working with Python list, we can have a problem in which we need to separate the float values from valid strings. But problem arises when float values are in form of strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + Exception Handling
8 min read