 Open in App
Not now

# Python – Filter Strings combination of K substrings

• Last Updated : 30 Aug, 2022

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)

My Personal Notes arrow_drop_up