Given a string P consisting of small English letters and a string Q consisting of weight of all characters of English alphabet such that for all ‘i’, 0 ≤ Q[i] ≤ 9. The task is to find the total numbers of unique substring with sum of weights atmost K.
Input: P = “ababab”, Q = “12345678912345678912345678”, K = 5
The substrings with the sum of weights of individual characters ≤ 5 are:
“a”, “ab”, “b”, “bc”, “c”, “d”, “e”
Input: P = “acbacbacaa”, Q = “12300045600078900012345000”, K = 2
The substrings with the sum of weights of individual characters ≤ 2 are:
“a”, “b”, “aa”
Approach: The idea is to use an unordered set to store the unique values. The following steps are followed to compute the answer:
- Iterate over all the substrings using the nested loops and maintain the sum of the weight of all the characters encountered so far.
- If the sum of characters is not greater than K, then insert it in a hashmap.
- Finally, output the size of the hashmap.
Below is the implementation of the above approach:
Time Complexity: O(N2)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count distinct substrings that contain some characters at most k times
- Count of all unique substrings with non-repeating characters
- Count of substrings of length K with exactly K distinct characters
- Longest substring with atmost K characters from the given set of characters
- Check if a string can be split into substrings starting with N followed by N characters
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Count Substrings with equal number of 0s, 1s and 2s
- Count of substrings of a binary string containing K ones
- Count number of distinct substrings of a given length
- Count of distinct substrings of a string using Suffix Array
- Count the number of vowels occurring in all the substrings of given string
- Count of distinct substrings of a string using Suffix Trie
- Queries to find the count of characters preceding the given location
- String Range Queries to count number of distinct characters with updates
- Replace minimal number of characters to make all characters pair wise distinct
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Strings formed from given characters without any consecutive repeating characters
- Longest subsequence having difference atmost K
- Longest subsequence such that absolute difference between every pair is atmost 1
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.