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
- Longest substring with count of 1s more than 0s
- Count the number of vowels occurring in all the substrings of given string
- Count distinct elements in an array
- Count subsets having distinct even numbers
- Shortest substring of a string containing all given words
- Count all distinct pairs with difference equal to k
- Count distinct elements in every window of size k
- Count distinct substrings that contain some characters at most k times
- Convert to a string that is repetition of a substring of k length
- Searching characters and substring in a String in Java
- Maximum length substring with highest frequency in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.