# Count of all sub-strings with sum of weights at most K

Given a string S consisting of small English letters and a string W consisting of weight of all characters of English alphabet where for all i, . We have to find the total numbers of a unique substring with sum of weights at most K.

**Examples:**

Input :P = “ababab”, Q = “12345678912345678912345678”, K=5

Output :7

Explanation :

The unique substrings are: “a”, “ab”, “b”, “bc”, “c”, “d”, “e”

Hence, the count is 7.

Input :P = “acbacbacaa”, Q = “12300045600078900012345000”, K=2

Output :3

Explanation :The unique substrings are: “a”, “b”, “aa”

Hence, the count is 3.

**Approach: **

To solve the above problem, the main idea is to simply iterate through all the substrings and maintain a sum of the weight of all characters encountered so far. If the sum of characters is not greater than K, then insert it in a hashmap otherwise discard it and move forward with another substring. Finally, the result will be the size of the hashmap because it stores all the substring which have weight less than or equal to K.

Below is the implementation of the above approach:

## C++

`// C++ implementation to Count all ` `// sub-strings with sum of weights at most K ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count all substrings ` `int` `distinctSubstring(string& P, string& Q, ` ` ` `int` `K, ` `int` `N) ` `{ ` ` ` ` ` `// Hashmap to store substrings ` ` ` `unordered_set<string> S; ` ` ` ` ` `// iterate over all substrings ` ` ` `for` `(` `int` `i = 0; i < N; ++i) { ` ` ` ` ` `// variable to maintain sum ` ` ` `// of all characters encountered ` ` ` `int` `sum = 0; ` ` ` ` ` `// variable to maintain ` ` ` `// substring till current position ` ` ` `string s; ` ` ` ` ` `for` `(` `int` `j = i; j < N; ++j) { ` ` ` `// get position of ` ` ` `// character in string W ` ` ` `int` `pos = P[j] - ` `'a'` `; ` ` ` ` ` `// add weight to current sum ` ` ` `sum += Q[pos] - ` `'0'` `; ` ` ` ` ` `// add current character to substring ` ` ` `s += P[j]; ` ` ` ` ` `// check if sum of characters ` ` ` `// is <=K insert in Hashmap ` ` ` `if` `(sum <= K) { ` ` ` `S.insert(s); ` ` ` `} ` ` ` `else` `{ ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `S.size(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// initialise string ` ` ` `string S = ` `"abcde"` `; ` ` ` ` ` `// initialise weight ` ` ` `string W = ` `"12345678912345678912345678"` `; ` ` ` ` ` `int` `K = 5; ` ` ` ` ` `int` `N = S.length(); ` ` ` ` ` `cout << distinctSubstring(S, W, K, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to Count all ` `# sub-strings with sum of weights at most K ` ` ` `# Function to count all substrings ` `def` `distinctSubstring(P, Q, K, N): ` ` ` ` ` `# Hashmap to store substrings ` ` ` `S ` `=` `set` `() ` ` ` ` ` `# iterate over all substrings ` ` ` `for` `i ` `in` `range` `(N): ` ` ` ` ` `# variable to maintain sum ` ` ` `# of all characters encountered ` ` ` `sum` `=` `0` ` ` ` ` `# variable to maintain ` ` ` `# substring till current position ` ` ` `s ` `=` `"" ` ` ` ` ` `for` `j ` `in` `range` `(i, N): ` ` ` ` ` `# get position of ` ` ` `# character in string W ` ` ` `pos ` `=` `ord` `(P[j]) ` `-` `97` ` ` ` ` `# add weight to current sum ` ` ` `sum` `+` `=` `ord` `(Q[pos]) ` `-` `48` ` ` ` ` `# add current character to substring ` ` ` `s ` `+` `=` `P[j] ` ` ` ` ` `# check if sum of characters ` ` ` `# is <=K insert in Hashmap ` ` ` `if` `(` `sum` `<` `=` `K): ` ` ` `S.add(s) ` ` ` ` ` `else` `: ` ` ` `break` ` ` ` ` `return` `len` `(S) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `# initialise string ` ` ` `S ` `=` `"abcde"` ` ` ` ` `# initialise weight ` ` ` `W ` `=` `"12345678912345678912345678"` ` ` ` ` `K ` `=` `5` ` ` ` ` `N ` `=` `len` `(S) ` ` ` ` ` `print` `(distinctSubstring(S, W, K, N)) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

7

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.

## Recommended Posts:

- Count all substrings having character K
- Count substrings with same first and last characters
- Count substrings that contain all vowels | SET 2
- Count of substrings having all distinct characters
- Count of total anagram substrings
- Count Substrings with equal number of 0s, 1s and 2s
- Count of substrings consisting only of vowels
- Count of substrings of a binary string containing K ones
- Number of substrings with count of each character as k
- Count of substrings formed using a given set of characters only
- Count of substrings which contains a given character K times
- Count of substrings of length K with exactly K distinct characters
- Count substrings with each character occurring at most k times
- Count number of distinct substrings of a given length
- Count distinct substrings that contain some characters at most k times
- Count of Palindromic substrings in an Index range
- Find the count of substrings in alphabetic order
- Count number of substrings with numeric value greater than X
- Recursive solution to count substrings with same first and last characters
- Count all Prime Length Palindromic Substrings

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.