Minimum cost to make a string free of a subsequence | C # implementation of the approach ; Function to return the minimum cost ; Traverse the string ; Min Cost to remove ' c ' ; Min Cost to remove subsequence " co " ; Min Cost to remove subsequence " cod " ; Min Cost to remove subsequence " code " ; Return the minimum cost ; Driver program
using System ; class GFG { public static int findCost ( string str , int [ ] arr , int n ) { long costofC = 0 , costofO = 0 , costofD = 0 , costofE = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' c ' ) { costofC += arr [ i ] ; } else if ( str [ i ] == ' o ' ) { costofO = Math . Min ( costofC , costofO + arr [ i ] ) ; } else if ( str [ i ] == ' d ' ) { costofD = Math . Min ( costofO , costofD + arr [ i ] ) ; } else if ( str [ i ] == ' e ' ) { costofE = Math . Min ( costofD , costofE + arr [ i ] ) ; } } return ( int ) costofE ; } public static void Main ( string [ ] args ) { string str = " geekcodergeeks " ; int [ ] arr = new int [ ] { 1 , 2 , 1 , 3 , 4 , 2 , 6 , 4 , 6 , 2 , 3 , 3 , 3 , 2 } ; int n = arr . Length ; Console . Write ( findCost ( str , arr , n ) ) ; } }