Check if both halves of the string have same set of characters | Javascript program to check if it two half of string contain same Character set or not ; Function to check both halves for equality ; Counter array initialized with 0 ; Length of the string ; Traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; String to be checked
let MAX_CHAR = 26 ; function checkCorrectOrNot ( s ) { let count = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } let n = s . length ; if ( n == 1 ) return true ; for ( let i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s [ i ] - ' ' ] ++ ; count [ s [ j ] - ' ' ] -- ; } for ( let i = 0 ; i < MAX_CHAR ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; } let s = " " ; if ( checkCorrectOrNot ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;