Given a string and a number k, the task is to find the number of substrings of length k whose sum of ASCII value of characters is divisible by k.
Input : str = “bcgabc”, k = 3
Output : 2
Substring “bcg” has sum of ASCII values 300 and “abc” has sum of ASCII values 294 which are divisible by 3.
Input : str = “adkf”, k = 3
Output : 1
Approach: First, we find the sum of ASCII value of characters of first substring of length k, then using sliding window technique subtract ASCII value of first character of the previous substring and add ASCII value of the current character. We will increase the counter at each step if sum is divisible by k.
Below is the implementation of above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Number of substrings with length divisible by the number of 1's in it
- Program to find the XOR of ASCII values of characters in a string
- Find the sum of the ascii values of characters which are present at prime positions
- Program to find the product of ASCII values of characters in a string
- Program to find the largest and smallest ASCII valued characters in a string
- Number of substrings divisible by 8 but not by 3
- Number of substrings divisible by 6 in a string of integers
- Count number of substrings with exactly k distinct characters
- Count number of substrings of a string consisting of same characters
- Count number of distinct substrings of a given length
- Find distinct characters in distinct substrings of a string
- Find length of longest substring with at most K normal characters
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Sub-strings having exactly k characters that have ASCII value greater than p
- Average of ASCII values of characters of a given 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.