Let’s say I have the string "Hey". I would like to determine all combinations of characters that exist in this string as fast as possible. The resulting algorithm should generate this:
H, e, y, He, ey, Hey
The algorithm should not produce the string "Hy" because it does not exist in the string as a substring.
There are
O(n^2)of those substrings, of lengths[1,n], so any algorithm to generate all of them will beO(n^2) * O(n) = O(n^3):(*) See Edit2 at the end – depending on the implementation of the string – the complexity can vary from
O(n^2)toO(n^3)Pseudo code:
Note however, that you can do some “cheating”, by creating an iterator and generate the substrings on the fly, it should look something like this [pseudo code]:
Note that creating the iterator is
O(1), and each iteration isO(n)– but to actually produce all the elements, you needO(n^2)steps, so complexity remainsO(n^3)overall, but you decrease the latency of your application.EDIT:
I editted complexity, claiming it is
O(n^2)is wrong, the complexity isO(n^3)since you need to generate strings of variable lengths, some of them are long. At least half of the generated substrings will be of lengthn/2– thus the total complexity isTheta(n^3)EDIT2:
In some cases it can actually be
O(n^2)– depending on the string implementation. In java for example – it uses a singlechar[], and only “plays” with theoffsetandlength– so in java – creation is actuallyO(n^2), since creating a substring isO(1)In C however – it is
O(n^3), since every substring needs to be copied to a differentchar[].