human
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"
Solution in Python:
assistant
#Here is code to modify string by replacing all occurrences of given characters by specified replacing characters and the other steps required.
 def replaceCharacters ( s , p ) :
	n , k = len ( s ) , len ( p )
	arr = [ 0 ] * 26
	brr = [ 0 ] * 26
	for i in range ( n ) :
		arr [ ord ( s [ i ] ) - ord ( ' a ' ) ] = s [ i ]
		brr [ ord ( s [ i ] ) - ord ( ' a ' ) ] = s [ i ]
	for j in range ( k ) :
		a , b = p [ j ] [ 0 ] , p [ j ] [ 1 ]
		for i in range ( 26 ) :
			if ( arr [ i ] == a ) :
				brr [ i ] = b
	for i in range ( n ) :
		print ( brr [ ord ( s [ i ] ) - ord ( ' a ' ) ] , end = " " )
if __name__ == ' _ _ main _ _ ' :
	S = " aabbgg "
	P = [ [ ' a ' , ' b ' ] , [ ' b ' , ' g ' ] , [ ' g ' , ' a ' ] ]
	replaceCharacters ( S , P )