text: Minimize cost of removals required to make all remaining characters of the string unique | C # program to implement the above approach ; Function to find the minimum cost of removing characters to make the string unique ; Stores the visited character ; Stores the answer ; Traverse the string ; If already visited ; Stores the maximum cost of removing a particular character ; Store the total deletion cost of a particular character ; Mark the current character visited ; Traverse the indices of the string [ i , N - 1 ] ; If any duplicate is found ; Update the maximum cost and total cost ; Mark the current character visited ; Keep the character with maximum cost and delete the rest ; Return the minimum cost ; Driver Code ; Input string ; Input array ; Function call
code: using System ; using System . Collections . Generic ; class GFG { public static int delCost ( string s , int [ ] cost ) { bool [ ] visited = new bool [ s . Length ] ; int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( visited [ i ] != false ) { continue ; } int maxDel = 0 ; int totalCost = 0 ; visited [ i ] = true ; for ( int j = i ; j < s . Length ; j ++ ) { if ( s [ i ] == s [ j ] ) { maxDel = Math . Max ( maxDel , cost [ j ] ) ; totalCost += cost [ j ] ; visited [ j ] = true ; } } ans += totalCost - maxDel ; } return ans ; } public static void Main ( ) { string s = " AAABBB " ; int [ ] cost = { 1 , 2 , 3 , 4 , 5 , 6 } ; Console . Write ( delCost ( s , cost ) ) ; } }