Find number of substrings of length k whose sum of ASCII value of characters is divisible by k | JavaScript 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
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 = " " ; var k = 3 ; var ans = count ( s , k ) ; document . write ( ans ) ;