Split a given string into substrings of length K with equal sum of ASCII values | Function for checking string ; Check if the string can be split into substrings of K length only ; Compute the sum of first substring of length K ; Compute the sum of remaining substrings ; Check if sum is equal to that of the first substring ; Since all sums are not equal , return false ; All sums are equal , Return true ; All substrings cannot be of size K ; Driver code
function check ( str , K ) { if ( str . length % K === 0 ) { var sum = 0 , i ; for ( i = 0 ; i < K ; i ++ ) { sum += str [ i ] . charCodeAt ( 0 ) ; } for ( var j = i ; j < str . length ; j += K ) { var s_comp = 0 ; for ( var p = j ; p < j + K ; p ++ ) s_comp += str [ p ] . charCodeAt ( 0 ) ; if ( s_comp !== sum ) return false ; } return true ; } return false ; } var K = 3 ; var str = " " ; if ( check ( str , K ) ) document . write ( " " ) ; else document . write ( " " ) ;