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 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)); } } |
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<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); } } } |
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 <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; } |
bc b abc ab aabc aa aaa c a aaab aab aaabc
Recommended Posts:
- Count distinct points visited on the number line
- Program to count number of distinct Squares and Cubes upto N
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Minimum length substring with exactly K distinct characters
- Count the number of vowels occurring in all the substrings of given string
- Longest substring with count of 1s more than 0s
- Count subsets having distinct even numbers
- Count distinct elements in an array
- Shortest substring of a string containing all given words
- Count distinct elements in every window of size k
- Count all distinct pairs with difference equal to k
- Count distinct substrings that contain some characters at most k times
- Count of pairs between two arrays such that the sums are distinct
- Convert to a string that is repetition of a substring of k length
- Searching characters and substring in a String in Java
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.



