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:
- 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
- Count number of substrings with exactly k distinct characters
- Number of substrings divisible by 6 in a string of integers
- Sub-strings having exactly k characters that have ASCII value greater than p
- Average of ASCII values of characters of a given string
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Count substrings with same first and last characters
- Count characters in a string whose ASCII values are prime
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
- Queries for frequencies of characters in substrings
- Recursive solution to count substrings with same first and last characters
- Count distinct substrings that contain some characters at most k times
- Convert all substrings of length 'k' from base 'b' to decimal
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.