Last active
June 2, 2017 22:40
-
-
Save mortymacs/fbce90bb27007830e4c5077fd56961ba to your computer and use it in GitHub Desktop.
Generate all substring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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