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 ; Utility function to print the minimized , replaced string ; Creating final string by replacing character ; index calculation ; Driver program
def minimize ( string ) : NEW_LINE INDENT mstr = " ▁ " NEW_LINE flagchar = [ 0 ] * 26 NEW_LINE l = len ( string ) NEW_LINE for i in range ( 0 , len ( string ) ) : NEW_LINE INDENT ch = string [ i ] NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if flagchar [ ord ( ch ) - 97 ] == 0 : NEW_LINE INDENT mstr = mstr + ch NEW_LINE flagchar [ ord ( ch ) - 97 ] = 1 NEW_LINE DEDENT DEDENT def replaceMinimizeUtil ( string ) : NEW_LINE INDENT finalStr = " " NEW_LINE l = len ( string ) NEW_LINE for i in range ( 0 , len ( minimizedStr ) ) : NEW_LINE INDENT ch = ord ( minimizedStr [ i ] ) NEW_LINE index = ( ch * ch ) % l NEW_LINE finalStr = finalStr + string [ index ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeks " NEW_LINE replaceMinimizeUtil ( string ) NEW_LINE DEDENT