Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
Last Updated :
08 Feb, 2024
Given a string and a number k, the task is to find the number of substrings of length k whose sum of the 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
Brute Force Approach:
The brute force approach to solve this problem is to generate all possible substrings of length k from the given string and calculate the sum of ASCII values of characters for each substring. Then check whether the sum is divisible by k or not. If it is divisible by k, then count it as a valid substring. Finally, return the count of valid substrings.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int count(string s, int k)
{
int n = s.length();
int cnt = 0;
for ( int i = 0; i <= n - k; i++)
{
int sum = 0;
for ( int j = i; j < i + k; j++)
sum += s[j];
if (sum % k == 0)
cnt++;
}
return cnt;
}
int main()
{
string s = "adkf" ;
int k = 3;
int ans = count(s, k);
cout << ans;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int count(String s, int k) {
int n = s.length();
int cnt = 0 ;
for ( int i = 0 ; i <= n - k; i++) {
int sum = 0 ;
for ( int j = i; j < i + k; j++)
sum += s.charAt(j);
if (sum % k == 0 )
cnt++;
}
return cnt;
}
public static void main(String[] args) {
String s = "adkf" ;
int k = 3 ;
int ans = count(s, k);
System.out.println(ans);
}
}
|
Python3
def count(s, k):
n = len (s)
cnt = 0
for i in range (n - k + 1 ):
_sum = 0
for j in range (i, i + k):
_sum + = ord (s[j])
if _sum % k = = 0 :
cnt + = 1
return cnt
s = "adkf"
k = 3
ans = count(s, k)
print (ans)
|
C#
using System;
class GFG {
public static int Count( string s, int k) {
int n = s.Length;
int cnt = 0;
for ( int i = 0; i <= n - k; i++) {
int sum = 0;
for ( int j = i; j < i + k; j++) {
sum += s[j];
}
if (sum % k == 0) {
cnt++;
}
}
return cnt;
}
public static void Main( string [] args) {
string s = "adkf" ;
int k = 3;
int ans = Count(s, k);
Console.WriteLine(ans);
}
}
|
Javascript
function count(s, k) {
let n = s.length;
let cnt = 0;
for (let i = 0; i <= n - k; i++) {
let sum = 0;
for (let j = i; j < i + k; j++) {
sum += s.charCodeAt(j);
}
if (sum % k === 0) {
cnt++;
}
}
return cnt;
}
let s = "adkf" ;
let k = 3;
let ans = count(s, k);
console.log(ans);
|
Time Complexity: O(N^2)
Space Complexity: O(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 the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int count(string s, int k)
{
int n = s.length();
int d = 0 ,i;
int count = 0 ;
for (i = 0; i <n; i++)
d += s[i] ;
if (d % k == 0)
count += 1 ;
for (i = k; i < n; i++)
{
int prev = s[i-k];
d -= prev;
d += s[i];
if (d % k == 0)
count += 1;
}
return count ;
}
int main()
{
string s = "bcgabc" ;
int k = 3 ;
int ans = count(s, k);
cout<<(ans);
}
|
Java
public class GFG{
static int count(String s, int k){
int n = s.length() ;
int d = 0 ,i;
int count = 0 ;
for (i = 0 ; i <n; i++)
d += s.charAt(i) ;
if (d % k == 0 )
count += 1 ;
for (i = k; i < n; i++)
{
int prev = s.charAt(i-k) ;
d -= prev ;
d += s.charAt(i) ;
if (d % k == 0 )
count += 1 ;
}
return count ;
}
public static void main(String[]args) {
String s = "bcgabc" ;
int k = 3 ;
int ans = count(s, k);
System.out.println(ans);
}
}
|
Python3
def count(s, k):
n = len (s)
d, count = 0 , 0
for i in range (k):
d + = ord (s[i])
if (d % k = = 0 ):
count + = 1
for i in range (k, n):
prev = ord (s[i - k])
d - = prev
d + = ord (s[i])
if (d % k = = 0 ):
count + = 1
return count
s = "bcgabc"
k = 3
ans = count(s, k)
print (ans)
|
C#
using System;
public class GFG{
static int count( string s, int k){
int n = s.Length ;
int d = 0 ,i;
int count = 0 ;
for (i = 0; i <n; i++)
d += s[i] ;
if (d % k == 0)
count += 1 ;
for (i = k; i < n; i++)
{
int prev = s[i-k] ;
d -= prev ;
d += s[i] ;
if (d % k == 0)
count += 1 ;
}
return count ;
}
public static void Main() {
string s = "bcgabc" ;
int k = 3 ;
int ans = count(s, k);
Console.Write(ans);
}
}
|
Javascript
<script>
function count(s, k)
{
var n = s.length;
var d = 0, i;
var count = 0;
for (i = 0; i < n; i++)
d += s[i].charCodeAt(0);
if (d % k === 0)
{
count += 1;
}
for (i = k; i < n; i++)
{
var prev = s[i - k];
d -= prev.charCodeAt(0);
d += s[i].charCodeAt(0);
if (d % k === 0)
count += 1;
}
return count;
}
var s = "bcgabc" ;
var k = 3;
var ans = count(s, k);
document.write(ans);
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...