Check if both halves of the string have at least one different character | PHP 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
< ? php $ MAX = 26 ; function function_1 ( $ str ) { global $ MAX ; $ l = strlen ( $ str ) ; $ counter1 = array_fill ( 0 , $ MAX , NULL ) ; $ counter2 = array_fill ( 0 , $ MAX , NULL ) ; for ( $ i = 0 ; $ i < $ l / 2 ; $ i ++ ) $ counter1 [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = $ l / 2 ; $ i < $ l ; $ i ++ ) $ counter2 [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) if ( $ counter2 [ $ i ] != $ counter1 [ $ i ] ) return true ; return false ; } $ str = " abcasdsabcae " ; if ( function_1 ( $ str ) ) echo " Yes , ▁ both ▁ halves ▁ differ " . " ▁ by ▁ at ▁ least ▁ one ▁ character " ; else echo " No , ▁ both ▁ halves ▁ do ▁ " . " not ▁ differ ▁ at ▁ all " ; return 0 ; ? >