Given a string str, the task is to find the count of distinct characters in all the distinct sub-strings of the given string.
Input: str = “ABCA”
Distinct sub-strings Distinct characters A 1 AB 2 ABC 3 ABCA 3 B 1 BC 2 BCA 3 C 1 CA 2
Hence, 1 + 2 + 3 + 3 + 1 + 2 + 3 + 1 + 2 = 18
Input: str = “AAAB”
Approach: Take all possible sub-strings of the given string and use a set to check whether the current sub-string has been processed before. Now, for every distinct sub-string, count the distinct characters in it (again set can be used to do so). The sum of this count for all the distinct sub-strings is the final answer.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count number of substrings with exactly k distinct characters
- Count distinct substrings that contain some characters at most k times
- Count of substrings of length K with exactly K distinct characters
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Count of substrings having all distinct characters
- String with k distinct characters and no same characters adjacent
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Count number of substrings of a string consisting of same characters
- Remove minimum characters from string to split it into three substrings under given constraints
- Check if a string can be split into substrings starting with N followed by N characters
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Count of substrings of a given Binary string with all characters same
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Count of distinct substrings of a string using Suffix Trie
- Count of distinct substrings of a string using Suffix Array
- Generate a String of having N*N distinct non-palindromic Substrings
- Count distinct substrings of a string using Rabin Karp algorithm
- Count of Distinct Substrings occurring consecutively in a given String
- Minimum changes to a string to make all substrings distinct
- Replace minimal number of characters to make all characters pair wise distinct
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.