Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting all combinations of a string and its substrings

Tags:

I've seen many questions on getting all the possible substrings (i.e., adjacent sets of characters), but none on generating all possible strings including the combinations of its substrings.

For example, let:

x = 'abc' 

I would like the output to be something like:

['abc', 'ab', 'ac', 'bc', 'a', 'b', 'c'] 

The main point is that we can remove multiple characters that are not adjacent in the original string (as well as the adjacent ones).

Here is what I have tried so far:

def return_substrings(input_string):     length = len(input_string)     return [input_string[i:j + 1] for i in range(length) for j in range(i, length)]  print(return_substrings('abc')) 

However, this only removes sets of adjacent strings from the original string, and will not return the element 'ac' from the example above.

Another example is if we use the string 'abcde', the output list should contain the elements 'ace', 'bd' etc.

like image 340
aL_eX Avatar asked Jul 26 '18 11:07

aL_eX


People also ask

How many substrings of a string are possible?

Approach: It is known for a string of length n, there are a total of n*(n+1)/2 number of substrings.

How do you find all substrings in length K?

If the length of a string is N, then there can be N – K + 1 substring of length K. Generating these substrings will require O(N) complexity, and checking each substring requires O(K) complexity, hence making the overall complexity like O(N*K). Efficient approach: The idea is to use Window Sliding Technique.

What are the substrings of a string?

In formal language theory and computer science, a substring is a contiguous sequence of characters within a string. For instance, "the best of" is a substring of "It was the best of times".


1 Answers

You can do this easily using itertools.combinations

>>> from itertools import combinations >>> x = 'abc' >>> [''.join(l) for i in range(len(x)) for l in combinations(x, i+1)] ['a', 'b', 'c', 'ab', 'ac', 'bc', 'abc'] 

If you want it in the reversed order, you can make the range function return its sequence in reversed order

>>> [''.join(l) for i in range(len(x),0,-1) for l in combinations(x, i)] ['abc', 'ab', 'ac', 'bc', 'a', 'b', 'c'] 
like image 177
Sunitha Avatar answered Sep 21 '22 03:09

Sunitha