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
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
- Count characters in a string whose ASCII values are prime
- Count substrings with same first and last characters
- Queries for frequencies of characters in substrings
- Count distinct substrings that contain some characters at most k times
- Recursive solution to count substrings with same first and last characters
- Convert all substrings of length 'k' from base 'b' to decimal
- Count all Prime Length Palindromic Substrings