# Count number of Distinct Substring in a String

Given a string, count all distinct substrings of the given string.

Examples

```Input : abcd
Output : abcd abc ab a bcd bc b cd c d
All Elements are Distinct

Input : aaa
Output : aaa aa a aa a a
All elements are not Distinct```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Prerequisite : Print subarrays of a given array

The idea is to use hash table (HashSet in Java) to store all generated substrings. Finally we return size of the HashSet.

 `// Java program to count all distinct substrings in a string ` `import` `java.util.HashSet; ` `import` `java.util.Iterator; ` `import` `java.util.Set; ` ` `  `public` `class` `DistinctSubstring { ` ` `  `    ``public` `static` `int` `distinctSubstring(String str) ` `    ``{ ` `        ``// Put all distinct substring in a HashSet ` `        ``Set result = ``new` `HashSet(); ` ` `  `        ``// List All Substrings ` `        ``for` `(``int` `i = ``0``; i <= str.length(); i++) { ` `            ``for` `(``int` `j = i + ``1``; j <= str.length(); j++) { ` ` `  `                ``// Add each substring in Set ` `                ``result.add(str.substring(i, j)); ` `            ``} ` `        ``} ` ` `  `        ``// Return size of the HashSet ` `        ``return` `result.size(); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String str = ``"aaaa"``; ` `        ``System.out.println(distinctSubstring(str)); ` `    ``} ` `} `

Output:

```4
```

How to print the distinct substrings?

 `// Java program to count all distinct substrings in a string ` `import` `java.util.HashSet; ` `import` `java.util.Iterator; ` `import` `java.util.Set; ` ` `  `public` `class` `DistinctSubstring { ` ` `  `    ``public` `static` `Set distinctSubstring(String str) ` `    ``{ ` ` `  `        ``// Put all distinct substring in a HashSet ` `        ``Set result = ``new` `HashSet(); ` ` `  `        ``// List All Substrings ` `        ``for` `(``int` `i = ``0``; i <= str.length(); i++) { ` `            ``for` `(``int` `j = i + ``1``; j <= str.length(); j++) { ` ` `  `                ``// Add each substring in Set ` `                ``result.add(str.substring(i, j)); ` `            ``} ` `        ``} ` ` `  `        ``// Return the HashSet ` `        ``return` `result; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String str = ``"aaaa"``; ` `        ``Set subs = distinctSubstring(str); ` ` `  `        ``System.out.println(``"Distinct Substrings are: "``); ` `        ``for` `(String s : subs) { ` `            ``System.out.println(s); ` `        ``} ` `    ``} ` `} `

Output:

```Distinct Substrings are:
aa
aaa
a
aaaa
```

Optimization:
We can further optimize the above code. The substr() function works in linear time. We can use append current character to previous substring to get the current substring.

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of ` `// valid sub-strings ` `void` `printSubstrings(string s) ` `{ ` ` `  `    ``// To store distinct output substrings ` `    ``unordered_set us; ` ` `  `    ``// Traverse through the given string and ` `    ``// one by one generate substrings beginning ` `    ``// from s[i]. ` `    ``for` `(``int` `i = 0; i < s.size(); ++i) { ` ` `  `        ``// One by one generate substrings ending ` `        ``// with s[j] ` `        ``string ss = ``""``; ` `        ``for` `(``int` `j = i; j < s.size(); ++j) { ` ` `  `            ``ss = ss + s[j]; ` `            ``us.insert(ss); ` `        ``} ` `    ``} ` ` `  `    ``// Print all substrings one by one ` `    ``for` `(``auto` `s : us) ` `        ``cout << s << ``" "``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"aaabc"``; ` `    ``printSubstrings(str); ` `    ``return` `0; ` `} `

Output:

```bc b abc ab aabc aa aaa c a aaab aab aaabc
```

My Personal Notes arrow_drop_up सर्वशक्तिशाली इकलौता

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.