Find all substrings of a String in java

In this post, we will see java program to find all substrings of a String.
For example: If input is “abb”  then output should be “a”, “b”,”b”, “ab”, “bb”, “abb”
We will use String class’s subString method to find all subString

Program:

When you run above program, you will get following output:

Above solution is of o(n^3) time complexity. As we have two loops and also String’s substring method has a time complexity of o(n)
If you want to find all distinct substrings of String,then use HashSet to remove duplicates.

Please go through Frequently asked java interview Programs for more such programs.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *