Modify string by replacing characters by alphabets whose distance from that character is equal to its frequency | Function to modify string by replacing characters by the alphabet present at distance equal to frequency of the string ; Stores frequency of characters ; Stores length of the string ; Traverse the given string S ; Increment frequency of current character by 1 ; Traverse the string ; Store the value to be added to the current character ; Check if after adding the frequency , the character is less than ' z ' or not ; Otherwise , update the value of add so that s [ i ] doesn ' t ▁ exceed ▁ ' z ' ; Print the modified string ; Driver Code
function addFrequencyToCharacter ( s ) { var frequency = new Array ( 26 ) . fill ( 0 ) ; var n = s . length ; 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 str = " " ; addFrequencyToCharacter ( str . split ( " " ) ) ;