Character replacement after removing duplicates from a string | Function to minimize String ; Duplicate characters are removed ; Checks if character has previously occurred or not if not then add it to the minimized String ' mstr ' ; Minimized String ; Utility function to print the minimized , replaced String ; Minimized String ; Creating final String by replacing character ; Index calculation ; Final String ; Driver code
function minimize ( str ) { let mstr = " " ; let l , i , flagchar = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { flagchar [ i ] = 0 ; } let ch ; l = str . length ; for ( i = 0 ; i < str . length ; i ++ ) { ch = str [ i ] ; if ( flagchar [ ch . charCodeAt ( 0 ) - 97 ] == 0 ) { mstr = mstr + ch ; flagchar [ ch . charCodeAt ( 0 ) - 97 ] = 1 ; } } return mstr ; } function replaceMinimizeUtil ( str ) { let minimizedStr , finalStr = " " ; let i , index , l ; let ch ; l = str . length ; minimizedStr = minimize ( str ) ; for ( i = 0 ; i < minimizedStr . length ; i ++ ) { ch = minimizedStr [ i ] . charCodeAt ( 0 ) ; index = ( ch * ch ) % l ; finalStr = finalStr + str [ index ] ; } document . write ( " " + finalStr ) ; } let str = " " ; replaceMinimizeUtil ( str ) ;