Minimum cost to make a string free of a subsequence | Function to return the Math . minimum cost ; Traverse the string ; Math . min Cost to remove ' c ' ; Math . min Cost to remove subsequence " co " ; Math . min Cost to remove subsequence " cod " ; Math . min Cost to remove subsequence " code " ; Return the Math . minimum cost ; Driver program
function findCost ( str , arr , n ) { var costofC = 0 , costofO = 0 , costofD = 0 , costofE = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) costofC += arr [ i ] ; else if ( str [ i ] == ' ' ) costofO = Math . min ( costofC , costofO + arr [ i ] ) ; else if ( str [ i ] == ' ' ) costofD = Math . min ( costofO , costofD + arr [ i ] ) ; else if ( str [ i ] == ' ' ) costofE = Math . min ( costofD , costofE + arr [ i ] ) ; } return costofE ; } var str = " " ; var arr = [ 1 , 2 , 1 , 3 , 4 , 2 , 6 , 4 , 6 , 2 , 3 , 3 , 3 , 2 ] ; var n = arr . length ; document . write ( findCost ( str , arr , n ) ) ;