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

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<String> result = new HashSet<String>();
  
        // 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));
    }
}

chevron_right


Output:

4

How to print the distinct substrings?

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<String> distinctSubstring(String str)
    {
  
        // Put all distinct substring in a HashSet
        Set<String> result = new HashSet<String>();
  
        // 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<String> subs = distinctSubstring(str);
  
        System.out.println("Distinct Substrings are: ");
        for (String s : subs) {
            System.out.println(s);
        }
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the count of
// valid sub-strings
void printSubstrings(string s)
{
  
    // To store distinct output substrings
    unordered_set<string> 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;
}

chevron_right


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.