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 Sorts the two halves separately Compares the two halves return true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function
#include <algorithm> NEW_LINE #include <cstring> NEW_LINE #include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; bool function ( char str [ ] ) { int l = strlen ( str ) ; sort ( str , str + ( l / 2 ) ) ; sort ( str + ( l / 2 ) , str + l ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) if ( str [ i ] != str [ l / 2 + i ] ) return true ; return false ; } int main ( ) { char 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 ; }