Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Last active June 2, 2017 22:40
Show Gist options
  • Select an option

  • Save mortymacs/fbce90bb27007830e4c5077fd56961ba to your computer and use it in GitHub Desktop.

Select an option

Save mortymacs/fbce90bb27007830e4c5077fd56961ba to your computer and use it in GitHub Desktop.
Generate all substring
def generate_substring(s):
t = [s, ]
for i in list(s):
if i not in t:
t.append(i)
for j in t:
if j.find(i) >= 0:
continue
x = i + j if i < j else j + i
if len(x) <= len(s) - 1 and x not in t:
t.append(x)
return t
# Example:
print(generate_substring("abcd"))
# Result:
['abcd', 'a', 'b', 'ab', 'c', 'ac', 'bc', 'abc', 'd', 'ad', 'bd', 'abd', 'cd', 'acd', 'bcd']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment