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.
This is a fun exercise. I think other answers may use itertools.product or itertools.combinations. But just for fun, you can also do this recursively with something like
@Sunitha answer provided the right tool to use. I will just go and suggest an improved way while using your
return_substrings
method. Basically, my solution will take care of duplicates.I will use
"ABCA"
in order to prove validity of my solution. Note that it would include a duplicate'A'
in the returned list of the accepted answer.Python 3.7+ solution,
Python 2.7 solution,
You'll have to use
OrderedDict
instead of a normaldict
. Therefore,becomes
Order is irrelevant for you ?
You can reduce code complexity if order is not relevant,
You can do this easily using
itertools.combinations
If you want it in the reversed order, you can make the
range
function return its sequence in reversed order