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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Split a given string into substrings of length K with equal sum of ASCII values
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k
- Count pairs of characters in a string whose ASCII value difference is K
- Count characters in a string whose ASCII values are prime
- Replace every character of string by character whose ASCII value is K times more than it
- Number of substrings with length divisible by the number of 1's in it
- Sub-strings having exactly k characters that have ASCII value greater than p
- Find the sum of the ascii values of characters which are present at prime positions
- Convert Hexadecimal value String to ASCII value String
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Program to find the largest and smallest ASCII valued characters in a string
- Program to find the product of ASCII values of characters in a string
- Program to find the XOR of ASCII values of characters in a string
- Count of substrings of length K with exactly K distinct characters
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Average of ASCII values of characters of a given string
- Count of alphabets having ASCII value less than and greater than k
- Convert the ASCII value sentence to its equivalent string
- Count and Print the alphabets having ASCII value in the range [l, r]
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.