Given a string, count all distinct substrings of the given string.
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.
How to print the distinct substrings?
Distinct Substrings are: aa aaa a aaaa
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.
bc b abc ab aabc aa aaa c a aaab aab aaabc
- 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 firstname.lastname@example.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.