Minimum cost to construct a string | C ++ 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 ; Driver Code ; s is the string that needs to be constructed
#include <iostream> NEW_LINE using namespace std ; int minCost ( string & s ) { bool alphabets [ 26 ] = { false } ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) alphabets [ s [ i ] - 97 ] = true ; int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) if ( alphabets [ i ] ) count ++ ; return count ; } int main ( ) { string s = " geeksforgeeks " ; cout << " Total ▁ cost ▁ to ▁ construct ▁ " << s << " ▁ is ▁ " << minCost ; return 0 ; }