text: Check if a string can be split into two strings with same number of K | Function to print the arrangement of characters ; Stores frequency of characters ; Count the character having frequency K ; Count the character having frequency greater than K and not equal to 2 K ; Case 1 ; Case 2 ; Case 3 ; If all cases fail ; Driver code
code: function DivideString ( s , n , k ) { var i , c = 0 , no = 1 ; var c1 = 0 , c2 = 0 ; var fr = new Array ( 26 ) . fill ( 0 ) ; var ans = [ ] ; for ( i = 0 ; i < n ; i ++ ) { fr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } var ch = " " , ch1 = " " ; for ( i = 0 ; i < 26 ; i ++ ) { if ( fr [ i ] === k ) { c ++ ; } if ( fr [ i ] > k && fr [ i ] !== 2 * k ) { c1 ++ ; ch = String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ; } if ( fr [ i ] === 2 * k ) { c2 ++ ; ch1 = String . fromCharCode ( i + " " . charCodeAt ( 0 ) ) ; } } for ( i = 0 ; i < n ; i ++ ) ans . push ( " " ) ; var mp = { } ; if ( c % 2 === 0 c1 > 0 c2 > 0 ) { for ( i = 0 ; i < n ; i ++ ) { if ( fr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === k ) { if ( mp . hasOwnProperty ( s [ i ] ) ) { ans [ i ] = " " ; } else { if ( no <= parseInt ( c / 2 ) ) { ans [ i ] = " " ; no ++ ; mp [ s [ i ] ] = 1 ; } } } } if ( c % 2 === 1 && c1 > 0 ) { no = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] === ch && no <= k ) { ans [ i ] = " " ; no ++ ; } } } if ( c % 2 === 1 && c1 === 0 ) { no = 1 ; var flag = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] === ch1 && no <= k ) { ans [ i ] = " " ; no ++ ; } if ( fr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === k && flag === 0 && ans [ i ] === " " ) { ans [ i ] = " " ; flag = 1 ; } } } document . write ( ans . join ( " " ) ) ; } else { document . write ( " " ) ; } } var S = " " ; var N = S . length ; var K = 1 ; DivideString ( S , N , K ) ;