Check if both halves of the string have at least one different character | Python3 implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Increments frequency of characters for first half Decrements frequency of characters for second half true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function
MAX = 26 NEW_LINE def function ( st ) : NEW_LINE INDENT global MAX NEW_LINE l = len ( st ) NEW_LINE counter = [ 0 ] * MAX NEW_LINE for i in range ( l // 2 ) : NEW_LINE INDENT counter [ ord ( st [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( l // 2 , l ) : NEW_LINE INDENT counter [ ord ( st [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE DEDENT for i in range ( MAX ) : NEW_LINE INDENT if ( counter [ i ] != 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT st = " abcasdsabcae " NEW_LINE if function ( st ) : NEW_LINE INDENT print ( " Yes , ▁ both ▁ halves ▁ differ ▁ by ▁ at ▁ " , " least ▁ one ▁ character " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No , ▁ both ▁ halves ▁ do ▁ not ▁ differ ▁ at ▁ all " ) NEW_LINE DEDENT