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
def replaceCharacters ( s , p ) : NEW_LINE INDENT n , k = len ( s ) , len ( p ) NEW_LINE arr = [ 0 ] * 26 NEW_LINE brr = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ ord ( s [ i ] ) - ord ( ' a ' ) ] = s [ i ] NEW_LINE brr [ ord ( s [ i ] ) - ord ( ' a ' ) ] = s [ i ] NEW_LINE DEDENT for j in range ( k ) : NEW_LINE INDENT a , b = p [ j ] [ 0 ] , p [ j ] [ 1 ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( arr [ i ] == a ) : NEW_LINE INDENT brr [ i ] = b NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( brr [ ord ( s [ i ] ) - ord ( ' a ' ) ] , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " aabbgg " NEW_LINE P = [ [ ' a ' , ' b ' ] , [ ' b ' , ' g ' ] , [ ' g ' , ' a ' ] ] NEW_LINE replaceCharacters ( S , P ) NEW_LINE DEDENT