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 ' ; return $mstr ; minimized string ; Utility function to print the minimized , replaced string ; Creating final string by replacing character ; index calculation ; echo " Final ▁ string : ▁ " . $finalStr ; final string ; Driver code
< ? php function minimize ( $ str ) { $ mstr = " ▁ " ; $ flagchar = array_fill ( 0 , 26 , 0 ) ; $ l = strlen ( $ str ) ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ ch = $ str [ $ i ] ; if ( $ flagchar [ ord ( $ ch ) - 97 ] == 0 ) { $ mstr . = $ ch ; $ flagchar [ ord ( $ ch ) - 97 ] = 1 ; } } } function replaceMinimizeUtil ( $ str ) { $ finalStr = " " ; $ l = strlen ( $ str ) ; for ( $ i = 0 ; $ i < strlen ( $ minimizedStr ) ; $ i ++ ) { $ ch = $ minimizedStr [ $ i ] ; $ index = ( ord ( $ ch ) * ord ( $ ch ) ) % $ l ; $ finalStr = $ finalStr . $ str [ $ index ] ; } } $ str = " geeks " ; replaceMinimizeUtil ( $ str ) ; ? >