Split a given string into substrings of length K with equal sum of ASCII values | Java program to check if a given string can be split into substrings of size K having an 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
class GFG { static boolean check ( String str , int K ) { if ( str . length ( ) % K == 0 ) { int sum = 0 , i ; for ( i = 0 ; i < K ; i ++ ) { sum += str . charAt ( i ) ; } for ( int j = i ; j < str . length ( ) ; j += K ) { int s_comp = 0 ; for ( int p = j ; p < j + K ; p ++ ) s_comp += str . charAt ( p ) ; if ( s_comp != sum ) return false ; } return true ; } return false ; } public static void main ( String args [ ] ) { int K = 3 ; String str = " abdcbbdba " ; if ( check ( str , K ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }