-37

I have a string in python, I need to find all the possible ways any substring of that string (including itself) could be selected. A substring (for my purposes) does not have to be contiguous, in the original string -- it could have gaps.
Eg: "frogman" is one of the many substrings of "froghuman' under this definition.

For example of the would function: If my string is "abcd", the output should be:

[ "a", "b", "c", "d", "ab", "ac", "ad", "bc", "bd", "cd", "abc", "abd", "acd", "bcd", "abcd" ]
Kartikey
  • 4,516
  • 4
  • 15
  • 40
lavee_singh
  • 1,379
  • 1
  • 13
  • 21
  • 2
    Hint: https://docs.python.org/2/library/itertools.html – Burhan Khalid Jan 11 '15 at 07:52
  • 1
    look at [`powerset()` itertools' recipe](https://docs.python.org/dev/library/itertools.html#itertools-recipes): `list(map(''.join, powerset('abcd')))` – jfs Jan 11 '15 at 08:19
  • @J.F.Sebastian I think your solution would also include 'ac' as an option (could be wrong). – user2539336 Jan 11 '15 at 08:40
  • Would the simplest way not just be to use a double for loop (in a comprehension or not), i.e. for i in range(n-1) for j in range(i+1,n) – user2539336 Jan 11 '15 at 08:42
  • 1
    @user2539336: look at the expected output in the question (`'ac'` is present). The correct term would be "subsequence" instead of "substring" here. – jfs Jan 11 '15 at 08:49
  • 13
    This question is not too broad. It's also being discussed on Meta: http://meta.stackoverflow.com/questions/283177/is-too-broad-a-valid-reason-to-close-a-question-that-doesnt-show-any-research – George Stocker Jan 11 '15 at 13:49

1 Answers1

28

Your example input/output suggests that you are looking for a power set. You could generate a power set for a string using itertools module in Python:

from itertools import chain, combinations

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

print(list(map(''.join, powerset('abcd'))))

Output

['',
 'a',
 'b',
 'c',
 'd',
 'ab',
 'ac',
 'ad',
 'bc',
 'bd',
 'cd',
 'abc',
 'abd',
 'acd',
 'bcd',
 'abcd']

Note: the output includes the empty string.

jfs
  • 399,953
  • 195
  • 994
  • 1,670