Find number of substrings of length k whose sum of ASCII value of characters is divisible by k | Java program to find number of substrings of length k whose sum of ASCII value of characters is divisible by k ; Finding length of string ; finding sum of ASCII value of first substring ; Using sliding window technique to find sum of ASCII value of rest of the substring ; checking if sum is divisible by k ; Driver code
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 ) ; } }