Minimum cost to construct a string | JavaScript Program to find minimum cost to construct a string ; Initially all characters are un - seen ; Marking seen characters ; Count total seen character , and that is the cost ; s is the string that needs to be constructed
function minCost ( s ) { var alphabets = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < s . length ; i ++ ) { alphabets [ s [ i ] . charCodeAt ( 0 ) - 97 ] = true ; } var count = 0 ; for ( var i = 0 ; i < 26 ; i ++ ) { if ( alphabets [ i ] ) { count ++ ; } } return count ; } var s = " " ; document . write ( " " + s + " " + minCost ( s . split ( " " ) ) ) ;