Modify string by replacing all occurrences of given characters by specified replacing characters | Function to modify given string by replacing characters ; Store the size of string and the number of pairs ; Initialize 2 character arrays ; Traverse the string s Update arrays arr [ ] and brr [ ] ; Traverse the array of pairs p ; a -> Character to be replaced b -> Replacing character ; Iterate over the range [ 0 , 25 ] ; If it is equal to current character , then replace it in the array b ; Print the array brr [ ] ; Driver code
function replaceCharacters ( s , p ) { var n = s . length , k = p . length ; var arr = new Array ( 26 ) . fill ( 0 ) ; var brr = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { arr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = s [ i ] ; brr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = s [ i ] ; } for ( var j = 0 ; j < k ; j ++ ) { var a = p [ j ] [ 0 ] , b = p [ j ] [ 1 ] ; for ( var i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] === a ) { brr [ i ] = b ; } } } for ( var i = 0 ; i < n ; i ++ ) { document . write ( brr [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ) ; } } var S = " " ; var P = [ [ " " , " " ] , [ " " , " " ] , [ " " , " " ] , ] ; replaceCharacters ( S , P ) ;