Character replacement after removing duplicates from a string | Java program for character replacement after String minimization ; 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 ; final String ; Driver program
class GFG { static String minimize ( String str ) { String mstr = " ▁ " ; int l , i , flagchar [ ] = new int [ 26 ] ; char ch ; l = str . length ( ) ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { ch = str . charAt ( i ) ; if ( flagchar [ ch - 97 ] == 0 ) { mstr = mstr + ch ; flagchar [ ch - 97 ] = 1 ; } } } static void replaceMinimizeUtil ( String str ) { String minimizedStr , finalStr = " " ; int i , index , l ; char ch ; l = str . length ( ) ; for ( i = 0 ; i < minimizedStr . length ( ) ; i ++ ) { ch = minimizedStr . charAt ( i ) ; index = ( ch * ch ) % l ; finalStr = finalStr + str . charAt ( index ) ; } System . out . println ( " Final ▁ String : ▁ " + finalStr ) ; } public static void main ( String [ ] args ) { String str = " geeks " ; replaceMinimizeUtil ( str ) ; } }