Check if both halves of the string have at least one different character | C ++ implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Counts frequency of characters in each half Compares the two counter array and returns true if these counter arrays differ ; Declaration and initialization of counter array ; Driver function
#include <cstring> NEW_LINE #include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; # define MAX  26 NEW_LINE bool function ( string str ) { int l = str . length ( ) ; int counter1 [ MAX ] ; int counter2 [ MAX ] ; memset ( counter1 , 0 , sizeof ( counter1 ) ) ; memset ( counter2 , 0 , sizeof ( counter2 ) ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) counter1 [ str [ i ] - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter2 [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) if ( counter2 [ i ] != counter1 [ i ] ) return true ; return false ; } int main ( ) { string str = " abcasdsabcae " ; if ( function ( str ) ) cout << " Yes , ▁ both ▁ halves ▁ differ " << " ▁ by ▁ at ▁ least ▁ one ▁ character " ; else cout << " No , ▁ both ▁ halves ▁ do ▁ " << " not ▁ differ ▁ at ▁ all " ; return 0 ; }