Python - Filter Strings combination of K substrings
Last Updated :
07 Apr, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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
Extract List of Substrings in List of Strings in Python
Working with strings is a fundamental aspect of programming, and Python provides a plethora of methods to manipulate and extract substrings efficiently. When dealing with a list of strings, extracting specific substrings can be a common requirement. In this article, we will explore five simple and c
3 min read
Python - Find all combinations of overlapping substrings of a string
Given a string, the task is to write a Python program to find all combinations of overlapping substrings of a string and store it in a list. The list of lists will be ordered and grouped by length of substrings. Input : test_str = 'Geeks4G' Output : [['', '', '', '', '', '', '', ''], ['G', 'e', 'e',
2 min read
Python - Ways to Count Number of Substring in String
Given a string s, determine the number of substrings that satisfy certain criteria. For example we are given a string s="hellohellohello" we need to count how many time the substring occur in the given string suppose we need to find the substring "hello" so that the count becomes 3. We can use metho
2 min read
Create List of Substrings from List of Strings in Python
In Python, when we work with lists of words or phrases, we often need to break them into smaller pieces, called substrings. A substring is a contiguous sequence of characters within a string. Creating a new list of substrings from a list of strings can be a common task in various applications. In th
3 min read
Count Occurance of Substring in a List of Strings - Python
To count the occurrences of a particular substring in a list of strings in Python, we can use several methods. In this article, we are going to explore different methods to count the existence of a particular substring in a given list.Using sum() and Generator ExpressionThis method uses a generator
2 min read
Python - Get all substrings of given string
A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element
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
Python - Possible Substring count from String
Given target string and argument substring, count how many substrings can be constructed using string characters, repetitions not allowed. Input : test_str = "geksefokesgergeeks", arg_str = "geeks" Output : 3 Explanation : "geeks" can be created 3 times using string characters. Input : test_str = "g
4 min read
Python | Get matching substrings in string
The testing of a single substring in a string has been discussed many times. But sometimes, we have a list of potential substrings and check which ones occur in a target string as a substring. Let's discuss certain ways in which this task can be performed. Method #1: Using list comprehension Using l
6 min read
Python | Find Mixed Combinations of string and list
Sometimes, while working with Python, we can have a problem in which we need to make combinations of string and character list. This type of problem can come in domains in which we need to interleave the data. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + e
5 min read