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 of substrings having all distinct characters
- Find the String having each substring with exactly K distinct characters
- Count number of substrings with exactly k distinct characters
- Count of substrings of length K with exactly K distinct characters
- Count distinct substrings that contain some characters at most k times
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Find the number of strings formed using distinct characters of a given string
- Minimum changes to a string to make all substrings distinct
- Generate a String of having N*N distinct non-palindromic Substrings
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Count of distinct substrings of a string using Suffix Trie
- Count of Distinct Substrings occurring consecutively in a given String
- Count of distinct substrings of a string using Suffix Array
- Count distinct substrings of a string using Rabin Karp algorithm
- String with k distinct characters and no same characters adjacent
- Convert given string so that it holds only distinct characters
- Check whether count of distinct characters in a string is Prime or not
- Contiguous subsegments of a string having distinct subsequent characters
- Print all distinct characters of a string in order (3 Methods)
- Count of Distinct strings possible by inserting K characters in the original 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 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.