Possible Duplicate:
How to find all substrings of a string in PHP
Find all subsets of a list
How can I compute all the possible substrings of a string? For example given a string ABCDE. All its possible substrings will be
A,
B,
C,
D,
E,
AB,
BC,
CD,
DE,
ABC,
BCD,
CDE,
ABCD,
BCDE,
ABCDE
Thanks! A pseudocode will be highly appreciated. 😀
Just use two for-loops:
You can also do it this way with two for-loops:
You probably want to include the empty string
""in the sequence you return as well, as it is a substring of all strings.You also need to consider if it is valid to yield a duplicate string multiple times (e.g. do you return “ABA” twice as a substring of “ABABA”?). If the answer is no, merely make a hashtable called
alreadyYielded, and whenever you yield, abort if you’ve already yielded the string, otherwise add the value to the hashtable in case you see it again. For example: