Check if both halves of the string have at least one different character | Java 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 code
import java . io . * ; import java . util . * ; class GFG { static Boolean function ( char str [ ] ) { int l = str . length ; Arrays . sort ( str , 0 , ( l / 2 ) ) ; Arrays . sort ( str , ( l / 2 ) , l ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) if ( str [ i ] != str [ l / 2 + i ] ) return true ; return false ; } public static void main ( String [ ] args ) { char str [ ] = ( " abcasdsabcae " ) . toCharArray ( ) ; if ( function ( str ) ) System . out . println ( " Yes , ▁ both ▁ halves ▁ differ " + " ▁ by ▁ at ▁ least ▁ one ▁ character " ) ; else System . out . println ( " No , ▁ both ▁ halves ▁ do " + " ▁ not ▁ differ ▁ at ▁ all " ) ; } }