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.

Java

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to count all distinct substrings in a string
  
def distinctSubstring(str):
    # Put all distinct substring in a HashSet
    result = set()
  
    # List All Substrings
    for i in range(len(str)+1):
        for j in range( i + 1, len(str)+1):
  
            # Add each substring in Set
            result.add(str[i:j]);
        # Return size of the HashSet
    return len(result);
  
# Driver Code
if __name__ == '__main__':
    str = "aaaa";
    print(distinctSubstring(str));
  
# This code has been contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count all distinct
// substrings in a string
using System;
using System.Collections.Generic;
  
class DistinctSubstring 
{
    public static int distinctSubstring(String str)
    {
        // Put all distinct substring in a HashSet
        HashSet<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 - i));
            }
        }
  
        // Return size of the HashSet
        return result.Count;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "aaaa";
        Console.WriteLine(distinctSubstring(str));
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

4

How to print the distinct substrings?

Java

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to count all distinct 
# substrings in a string
  
def distinctSubstring(str):
  
    # Put all distinct substring in a HashSet
    result = set();
  
    # List All Substrings
    for i in range(len(str)):
        for j in range(i + 1, len(str) + 1):
  
            # Add each substring in Set
            result.add(str[i:j]);
  
        # Return the HashSet
    return result;
  
# Driver Code
if __name__ == '__main__':
  
    str = "aaaa";
    subs = distinctSubstring(str);
  
    print("Distinct Substrings are: ");
    for s in subs:
        print(s);
  
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to count all distinct 
// substrings in a string
using System;
using System.Collections.Generic;
  
class GFG
{
    public static HashSet<String> distinctSubstring(String str)
    {
  
        // Put all distinct substring in a HashSet
        HashSet<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 - i));
            }
        }
  
        // Return the HashSet
        return result;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "aaaa";
        HashSet<String> subs = distinctSubstring(str);
  
        Console.WriteLine("Distinct Substrings are: ");
        foreach (String s in subs) 
        {
            Console.WriteLine(s);
        }
    }
}
  
// This code is contributed by 29AjayKumar

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.

C++

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function to return the count of
// valid sub-Strings
static void printSubStrings(String s)
{
  
    // To store distinct output subStrings
    HashSet<String> us = new HashSet<String>();
  
    // Traverse through the given String and
    // one by one generate subStrings beginning
    // from s[i].
    for (int i = 0; i < s.length(); ++i)
    {
  
        // One by one generate subStrings ending
        // with s[j]
        String ss = "";
        for (int j = i; j < s.length(); ++j) 
        {
            ss = ss + s.charAt(j);
            us.add(ss);
        }
    }
  
    // Print all subStrings one by one
    for (String str : us)
        System.out.print(str + " ");
}
  
// Driver code
public static void main(String[] args)
{
    String str = "aaabc";
    printSubStrings(str);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the count of
# valid sub-Strings
def printSubStrings(s):
  
    # To store distinct output subStrings
    us = set();
  
    # Traverse through the given String and
    # one by one generate subStrings beginning
    # from s[i].
    for i in range(len(s)):
  
        # One by one generate subStrings ending
        # with s[j]
        ss = "";
        for j in range(i, len(s)):
            ss = ss + s[j];
            us.add(ss);
          
    # Prall subStrings one by one
    for str in us:
        print(str, end=" ");
  
# Driver code
if __name__ == '__main__':
    str = "aaabc";
    printSubStrings(str);
      
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function to return the count of
// valid sub-Strings
static void printSubStrings(String s)
{
  
    // To store distinct output subStrings
    HashSet<String> us = new HashSet<String>();
  
    // Traverse through the given String and
    // one by one generate subStrings 
    // beginning from s[i].
    for (int i = 0; i < s.Length; ++i)
    {
  
        // One by one generate subStrings
        // ending with s[j]
        String ss = "";
        for (int j = i; j < s.Length; ++j) 
        {
            ss = ss + s[j];
            us.Add(ss);
        }
    }
  
    // Print all subStrings one by one
    foreach (String str in us)
        Console.Write(str + " ");
}
  
// Driver code
public static void Main(String[] args)
{
    String str = "aaabc";
    printSubStrings(str);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : 29AjayKumar, Rajput-Ji