Find number of substrings of length k whose sum of ASCII value of characters is divisible by k

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.

Examples:

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find number of substrings
// of length k whose sum of ASCII value of 
// characters is divisible by k 
#include<bits/stdc++.h>
using namespace std;
  
int count(string s, int k)
{
      
    // Finding length of string
    int n = s.length(); 
    int d = 0 ,i;
    int count = 0 ;
      
    for (i = 0; i <n; i++)
        // finding sum of ASCII value of first 
        // substring
        d += s[i] ;
      
    if (d % k == 0)
        count += 1 ;
      
    for (i = k; i < n; i++)
    {
      
        // Using sliding window technique to 
        // find sum of ASCII value of rest of 
        // the substring
        int prev = s[i-k];
        d -= prev;
        d += s[i];
      
      
      
        // checking if sum is divisible by k
        if (d % k == 0)
        count += 1;
    }
      
    return count ;
    }
      
    // Driver code
    int main()
    {
          
        string s = "bcgabc" ;
        int k = 3 ;
        int ans = count(s, k); 
        cout<<(ans); 
    }
    // This code is contributed by 
    // Sahil_Shelangia

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find number of substrings
// of length k whose sum of ASCII value of 
// characters is divisible by k 
  
  
public class GFG{
  
    static int count(String s, int k){
      
    // Finding length of string
    int n = s.length() ; 
    int d = 0 ,i;
    int count = 0 ;
      
    for (i = 0; i <n; i++)
        // finding sum of ASCII value of first 
        // substring
        d += s.charAt(i) ;
      
    if (d % k == 0)
        count += 1 ;
      
    for (i = k; i < n; i++)
    {
      
        // Using sliding window technique to 
        // find sum of ASCII value of rest of 
        // the substring
        int prev = s.charAt(i-k) ;
        d -= prev ;
        d += s.charAt(i) ;
      
      
      
        // checking if sum is divisible by k
        if (d % k == 0)
        count += 1 ;
    }
      
    return count ;
    }
      
    // Driver code
    public static void main(String[]args) {
          
        String s = "bcgabc" ;
        int k = 3 ;
        int ans = count(s, k); 
        System.out.println(ans); 
    }
    // This code is contributed by Ryuga
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find number of substrings
# of length k whose sum of ASCII value of 
# characters is divisible by k 
  
def count(s, k):
      
    # Finding length of string
    n = len(s) 
    d, count = 0, 0
    for i in range(k): 
          
        # finding sum of ASCII value of first 
        # substring
        d += ord(s[i])
        if (d % k == 0):
            count += 1
            for i in range(k, n):
                  
                # Using sliding window technique to 
                # find sum of ASCII value of rest of 
                # the substring
                prev = ord(s[i-k])
                d -= prev
                d += ord(s[i])
                  
                # checking if sum is divisible by k
                if (d % k == 0):
                    count += 1
                    return count
# Driver code
s = "bcgabc"
k = 3
ans = count(s, k) 
print(ans) 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find number of substrings
// of length k whose sum of ASCII value of 
// characters is divisible by k 
   
   
using System;
public class GFG{
   
    static int count(string s, int k){
       
    // Finding length of string
    int n = s.Length ; 
    int d = 0 ,i;
    int count = 0 ;
       
    for (i = 0; i <n; i++)
        // finding sum of ASCII value of first 
        // substring
        d += s[i] ;
       
    if (d % k == 0)
        count += 1 ;
       
    for (i = k; i < n; i++)
    {
       
        // Using sliding window technique to 
        // find sum of ASCII value of rest of 
        // the substring
        int prev = s[i-k] ;
        d -= prev ;
        d += s[i] ;
       
       
       
        // checking if sum is divisible by k
        if (d % k == 0)
        count += 1 ;
    }
       
    return count ;
    }
       
    // Driver code
    public static void Main() {
           
        string s = "bcgabc" ;
        int k = 3 ;
        int ans = count(s, k); 
        Console.Write(ans); 
    }
    
}

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Computer Science enthusiast

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.