text: Minimize cost of removals required to make all remaining characters of the string unique | C # program for the above approach ; Function to find the minimum cost of removing characters to make the string unique ; Store the minimum cost required ; Create a dictionary to store the maximum cost of removal a character ; Create a dictionary to store the total deletion cost of a character ; Traverse the string , S ; Keep track of maximum cost of each character ; Update the maximum deletion cost ; Keep track of the total cost of each character ; Update the total deletion cost ; Traverse through all the unique characters ; Keep the maximum cost character and delete the rest ; Return the answer ; Driver code ; Given string ; Given cost array ; Function Call
code: using System ; using System . Collections . Generic ; class GFG { static int delCost ( string s , int [ ] cost ) { int ans = 0 ; Dictionary < int , int > forMax = new Dictionary < int , int > ( ) ; Dictionary < int , int > forTot = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! forMax . ContainsKey ( s [ i ] ) ) { forMax [ s [ i ] ] = cost [ i ] ; } else { forMax [ s [ i ] ] = Math . Max ( cost [ i ] , forMax [ s [ i ] ] ) ; } if ( ! forTot . ContainsKey ( s [ i ] ) ) { forTot [ s [ i ] ] = cost [ i ] ; } else { forTot [ s [ i ] ] += cost [ i ] ; } } foreach ( KeyValuePair < int , int > i in forMax ) { ans += forTot [ i . Key ] - i . Value ; } return ans ; } static void Main ( ) { string s = " AAABBB " ; int [ ] cost = { 1 , 2 , 3 , 4 , 5 , 6 } ; Console . WriteLine ( delCost ( s , cost ) ) ; } }