Character replacement after removing duplicates from a string | C ++ 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 ; cout << " Final ▁ string : ▁ " << finalStr ; final string ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string minimize ( string str ) { string mstr = " ▁ " ; int l , i , flagchar [ 26 ] = { 0 } ; char ch ; l = str . length ( ) ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { ch = str . at ( i ) ; if ( flagchar [ ch - 97 ] == 0 ) { mstr = mstr + ch ; flagchar [ ch - 97 ] = 1 ; } } } 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 . at ( i ) ; index = ( ch * ch ) % l ; finalStr = finalStr + str . at ( index ) ; } } int main ( ) { string str = " geeks " ; replaceMinimizeUtil ( str ) ; return 0 ; }