explain the fastest way to find all the substrings of a given string with the help of example?


The most basic approach to this would be an O(N^2) algorithm. Something like this,

    String str;
    for (int i = 0; i < Str.length(); i++) {
        for (int j = i+1; j <= Str.length(); j++) {
            //print substring of Str which is (i,j)

I don’t think there will be any other way to do this because if there are n^2 strings then it should take a minimum of n^2 iterations, mathematically! (Sorry cant provide you any proof though, struck me logically).

But depending upon the problem that you’re solving some modifications maybe possible!

The most common one is to use Suffix Tree.

Hope this helps! :slight_smile: