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
def check ( str , K ) : NEW_LINE INDENT if ( len ( str ) % K == 0 ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( K ) : NEW_LINE INDENT sum += ord ( str [ i ] ) ; NEW_LINE DEDENT for j in range ( K , len ( str ) , K ) : NEW_LINE INDENT s_comp = 0 ; NEW_LINE for p in range ( j , j + K ) : NEW_LINE INDENT s_comp += ord ( str [ p ] ) ; NEW_LINE DEDENT if ( s_comp != sum ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT K = 3 ; NEW_LINE str = " abdcbbdba " ; NEW_LINE if ( check ( str , K ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT