text: Minimize cost of removals required to make all remaining characters of the string unique | 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 ; input string ; input array ; function call
code: function delCost ( s , cost ) { var visited = Array ( s . length ) . fill ( false ) ; var ans = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( visited [ i ] ) { continue ; } var maxDel = 0 ; var totalCost = 0 ; visited [ i ] = true ; for ( j = i ; j < s . length ; j ++ ) { if ( s . charAt ( i ) == s . charAt ( j ) ) { maxDel = Math . max ( maxDel , cost [ j ] ) ; totalCost += cost [ j ] ; visited [ j ] = true ; } } ans += totalCost - maxDel ; } return ans ; } var s = " " ; var cost = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; document . write ( delCost ( s , cost ) ) ;