Minimum cost to construct a string | Java 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
class GFG { static int minCost ( char [ ] s ) { boolean alphabets [ ] = new boolean [ 26 ] ; for ( int i = 0 ; i < s . length ; i ++ ) { alphabets [ ( int ) s [ i ] - 97 ] = true ; } int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( alphabets [ i ] ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( " Total ▁ cost ▁ to ▁ construct ▁ " + s + " ▁ is ▁ " + minCost ( s . toCharArray ( ) ) ) ; } }