# Minimum cost to construct a string

Given a string s (containing lowercase letters only), we have to find the minimum cost to construct the given string. The cost can determined using the following operations:

1. Appending a single character cost 1 unit

2. A sub-string of new string(intermediate string) can be appended without any cost

Note* Intermediate string is the string formed so far.

**Examples:**

Input : "geks" Output : cost: 4 Explanation: appending 'g' cost 1, string "g" appending 'e' cost 1, string "ge" appending 'k' cost 1, string "gek" appending 's' cost 1, string "geks" Hence, Total cost to construct "geks" is 4 Input : "abab" Output : cost: 2 Explanation: Appending 'a' cost 1, string "a" Appending 'b' cost 1, string "ab" Appending "ab" cost nothing as it is substring of intermediate. Hence, Total cost to construct "abab" is 2

**Naive Approach**: Check if there is sub-string in the remaining string to be constructed which is also a sub-string in the intermediate string, if there is then append it at no cost and if not then append it at the cost of 1 unit per character.

In the above example when intermediate string was “ab” and we need to construct “abab” then remaining string was “ab”. Hence there is a sub-string in remaining string which is also a sub-string of intermediate string (i.e. “ab”) and therefore cost us nothing.

**Better Approach**: We will use hashing technique, to maintain that whether we have seen a character or not. If we have seen the character, then there is no cost to append the character and if not, then it cost us 1 unit.

Now in this approach we take one character at a time and not a string. This is because if “ab” is substring of “abab”, so is ‘a’ and ‘b’ alone and hence make no difference.

This also leads us to the conclusion that the cost to construct a string is never more than 26 in case the string contains all the alphabets (a-z).

## C++

`// C++ Program to find minimum cost to ` `// construct a string ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `minCost(string& s) ` `{ ` ` ` `// Initially all characters are un-seen ` ` ` `bool` `alphabets[26] = { ` `false` `}; ` ` ` ` ` `// Marking seen characters ` ` ` `for` `(` `int` `i = 0; i < s.size(); i++) ` ` ` `alphabets[s[i] - 97] = ` `true` `; ` ` ` ` ` `// Count total seen character, and that ` ` ` `// is the cost ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < 26; i++) ` ` ` `if` `(alphabets[i]) ` ` ` `count++; ` ` ` ` ` `return` `count; ` `} ` ` ` `int` `main() ` `{ ` ` ` `// s is the string that needs to be constructed ` ` ` `string s = ` `"geeksforgeeks"` `; ` ` ` ` ` `cout << ` `"Total cost to construct "` ` ` `<< s << ` `" is "` `<< minCost; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find minimum cost to ` `// construct a string ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `int` `minCost(` `char` `[] s) ` ` ` `{ ` ` ` ` ` `// Initially all characters are un-seen ` ` ` `boolean` `alphabets[] = ` `new` `boolean` `[` `26` `]; ` ` ` ` ` `// Marking seen characters ` ` ` `for` `(` `int` `i = ` `0` `; i < s.length; i++) ` ` ` `{ ` ` ` `alphabets[(` `int` `) s[i] - ` `97` `] = ` `true` `; ` ` ` `} ` ` ` ` ` `// Count total seen character, ` ` ` `// and that is the cost ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) ` ` ` `{ ` ` ` `if` `(alphabets[i]) ` ` ` `{ ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// s is the string that needs to be constructed ` ` ` `String s = ` `"geeksforgeeks"` `; ` ` ` `System.out.println(` `"Total cost to construct "` `+ ` ` ` `s + ` `" is "` `+ minCost(s.toCharArray())); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 Program to find minimum cost to ` `# construct a string ` ` ` `def` `minCost(s): ` ` ` ` ` `# Initially all characters are un-seen ` ` ` `alphabets ` `=` `[` `False` `for` `i ` `in` `range` `(` `26` `)] ` ` ` ` ` `# Marking seen characters ` ` ` `for` `i ` `in` `range` `(` `len` `(s)): ` ` ` `alphabets[` `ord` `(s[i]) ` `-` `97` `] ` `=` `True` ` ` ` ` `# Count total seen character, and that ` ` ` `# is the cost ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(` `26` `): ` ` ` `if` `(alphabets[i]): ` ` ` `count ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# s is the string that needs to ` ` ` `# be constructed ` ` ` `s ` `=` `"geeksforgeeks"` ` ` ` ` `print` `(` `"Total cost to construct"` `, s, ` ` ` `"is"` `, minCost(s)) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find minimum cost to ` `// construct a string ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `int` `minCost(` `char` `[] s) ` ` ` `{ ` ` ` ` ` `// Initially all characters are un-seen ` ` ` `bool` `[]alphabets = ` `new` `bool` `[26]; ` ` ` ` ` `// Marking seen characters ` ` ` `for` `(` `int` `i = 0; i < s.Length; i++) ` ` ` `{ ` ` ` `alphabets[(` `int` `) s[i] - 97] = ` `true` `; ` ` ` `} ` ` ` ` ` `// Count total seen character, ` ` ` `// and that is the cost ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < 26; i++) ` ` ` `{ ` ` ` `if` `(alphabets[i]) ` ` ` `{ ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `// s is the string that ` ` ` `// needs to be constructed ` ` ` `String s = ` `"geeksforgeeks"` `; ` ` ` `Console.WriteLine(` `"Total cost to construct "` `+ ` ` ` `s + ` `" is "` `+ minCost(s.ToCharArray())); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

Total cost to construct geeksforgeeks is 7

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum cost to modify a string
- Minimum cost to traverse from one index to another in the String
- Minimum cost to convert string into palindrome
- Minimum cost to make a string free of a subsequence
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Minimum cost to make two strings same
- Minimum Cost To Make Two Strings Identical
- Construct a binary string following the given constraints
- Minimum Cost to make two Numeric Strings Identical
- Minimum cost to convert str1 to str2 with the given operations
- Choose atleast two elements from array such that their GCD is 1 and cost is minimum
- Construct the Cypher string based on the given conditions
- Minimum cost to sort strings using reversal operations of different costs
- Minimum cost to make two strings identical by deleting the digits
- Count the number of ways to construct the target string
- Construct Binary Tree from String with bracket representation
- Cost to make a string Panagram | Set 2
- Cost to make a string Panagram
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Minimum deletions from string to reduce it to string with at most 2 unique characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.