Open In App

Python – Filter Strings combination of K substrings

Last Updated : 07 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 = [“geeks4u”, “allbest”, “abcdef”], substr_list = [“s4u”, “est”, “al”, “ge”, “def”, ‘lb’], K = 3 

Output : [‘allbest’] 

Explanation : allbest made up of 3 substr – al, lb and est.

Method #1 : Using permutations() + map() + join() + set() + loop

In this, we perform this task by getting all possible permutation of K substrings from substring list, and then perform task of join using join and map(). The set() is used to avoid duplication. At last, match from Strings list is done using loop.

Python3




# Python3 code to demonstrate working of
# Filter Strings  combination of K substrings
# Using permutations() + map() + join() + set() + loop
from itertools import permutations
 
# initializing list
test_list = ["geeks4u", "allbest", "abcdef"]
 
# printing string
print("The original list : " + str(test_list))
 
# initializing substring list
substr_list = ["s4u", "est", "al", "ge", "ek", "def", "lb"]
 
# initializing K
K = 3
 
# getting all permutations
perms = list(set(map(''.join, permutations(substr_list, r = K))))
 
# using loop to check permutations with list
res = []
for ele in perms:
    if ele in test_list:
        res.append(ele)
 
# printing results
print("Strings after joins : " + str(res))


Output

The original list : ['geeks4u', 'allbest', 'abcdef']
Strings after joins : ['geeks4u', 'allbest']

Method #2 : Using intersection()

This uses all functions of the above method, the last task of matching permutation list and original list is done by intersection.

Python3




# Python3 code to demonstrate working of
# Filter Strings  combination of K substrings
# Using permutations() + map() + join() + set() + intersection()
from itertools import permutations
 
# initializing list
test_list = ["geeks4u", "allbest", "abcdef"]
 
# printing string
print("The original list : " + str(test_list))
 
# initializing substring list
substr_list = ["s4u", "est", "al", "ge", "ek", "def", "lb"]
 
# initializing K
K = 3
 
# getting all permutations
perms = set(map(''.join, permutations(substr_list, r = K)))
 
# using intersection() to solve this problem
res = list(set(test_list).intersection(perms))
 
# printing results
print("Strings after joins : " + str(res))


Output

The original list : ['geeks4u', 'allbest', 'abcdef']
Strings after joins : ['geeks4u', 'allbest']

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)

Method 3: Using nested loops to generate permutations and checking for matches.

Algorithm:
1. Import the `permutations()` function from `itertools`.
2. Define the given `test_list`, `substr_list`, and `K`.
3. Initialize an empty list `res`.
4. Loop through each string `s` in `test_list`.
5. Loop through each permutation `p` of length `K` in `substr_list`.
6. Check if the joined permutation `p` is a substring of the current string `s`.
7. If it is, append the string `s` to the `res` list and break out of the loop over permutations.
8. Print the resulting list `res` of strings that contain at least one combination of `K` substrings from `substr_list`.

Python3




from itertools import permutations
 
test_list = ["geeks4u", "allbest", "abcdef"]
substr_list = ["s4u", "est", "al", "ge", "ek", "def", "lb"]
K = 3
print("The original list : " + str(test_list))
 
res = []
for s in test_list:
    for p in permutations(substr_list, K):
        if ''.join(p) in s:
            res.append(s)
            break
 
print("Strings after joins : " + str(res))


Output

The original list : ['geeks4u', 'allbest', 'abcdef']
Strings after joins : ['geeks4u', 'allbest']

The time complexity of this algorithm is O(n*k! * m), where n is the length of `test_list`, k is the length of each permutation (which is a constant `K` in this case), and m is the maximum length of any string in `test_list`.
The auxiliary space of this algorithm is also O(n * m), since we need to store the resulting list `res` of strings that match the condition. The space complexity of the `permutations()` function is O(k!), which is a constant.



Similar Reads

Python - Find all the strings that are substrings to the given list of strings
Given two lists, the task is to write a Python program to extract all the strings which are possible substring to any of strings in another list. Example: Input : test_list1 = ["Geeksforgeeks", "best", "for", "geeks"], test_list2 = ["Geeks", "win", "or", "learn"] Output : ['Geeks', 'or'] Explanation : "Geeks" occurs in "Geeksforgeeks string as subs
5 min read
Spatial Filters - Averaging filter and Median filter in Image Processing
Spatial Filtering technique is used directly on pixels of an image. Mask is usually considered to be added in size so that it has a specific center pixel. This mask is moved on the image such that the center of the mask traverses all image pixels.In this article, we are going to cover the following topics - To write a program in Python to implement
3 min read
Python | Filter a list based on the given list of strings
Given a List, the task is to filter elements from list based on another list of strings. These type of problems are quite common while scraping websites. Examples: Input: List_string1 = ['key', 'keys', 'keyword', 'keychain', 'keynote'] List_string2 = ['home/key/1.pdf', 'home/keys/2.pdf', 'home/keyword/3.pdf', 'home/keychain/4.pdf', 'home/Desktop/5.
3 min read
Python | Filter list of strings based on the substring list
Given two lists of strings string and substr, write a Python program to filter out all the strings in string that contains string in substr. Examples: Input : string = ['city1', 'class5', 'room2', 'city2']substr = ['class', 'city']Output : ['city1', 'class5', 'city2'] Input : string = ['coordinates', 'xyCoord', '123abc']substr = ['abc', 'xy']Output
8 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 The combination of above functionalities can be us
8 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. Method #1: Using list comprehension + len() The com
5 min read
Python - Filter Tuples with Strings of specific characters
Given a Tuple List, extract tuples, which have strings made up of certain characters. Input : test_list = [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg')], char_str = 'gfestb' Output : [('gfg', 'best'), ('fest', 'gfg')] Explanation : All tuples contain characters from char_str. Input : test_list = [('gfg', 'best'), ('gfg', 'good'), ('fest', 'gfg
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", "best"], ["gfg ", "good"], ["gfg is cool"], ["l
6 min read
Python - Filter Similar Case Strings
Given the Strings list, the task is to write a Python program to filter all the strings which have a similar case, either upper or lower. Examples: Input : test_list = ["GFG", "Geeks", "best", "FOr", "all", "GEEKS"] Output : ['GFG', 'best', 'all', 'GEEKS'] Explanation : GFG is all uppercase, best is all lowercase. Input : test_list = ["GFG", "Geeks
9 min read
Python - Filter Supersequence Strings
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
4 min read
Practice Tags :