Modify a string by circularly shifting each character to the right by respective frequencies | Function to replace the characters by its frequency of character in it ; Stores frequencies of characters in the string S ; Traverse the string S ; Increment the frequency of each character by 1 ; Traverse the string S ; Find the frequency of the current character ; Update the character ; Print the resultant string ; Driver Code
function addFrequencyToCharacter ( Str ) { var frequency = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; var N = Str . length ; var S = Str . split ( ' ' ) ; for ( var i = 0 ; i < N ; i ++ ) { frequency [ S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; } for ( var i = 0 ; i < N ; i ++ ) { var add = frequency [ S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] % 26 ; if ( ( S [ i ] . charCodeAt ( 0 ) ) + add <= ( ' ' ) . charCodeAt ( 0 ) ) S [ i ] = String . fromCharCode ( ( S [ i ] . charCodeAt ( 0 ) ) + add ) ; else { add = ( ( S [ i ] . charCodeAt ( 0 ) ) + add ) - ( ( ' ' ) . charCodeAt ( 0 ) ) ; S [ i ] = String . fromCharCode ( ( ' ' . charCodeAt ( 0 ) ) + add - 1 ) ; } } document . write ( S . join ( ' ' ) ) ; } var S = " " ; addFrequencyToCharacter ( S ) ;