GeeksforGeeks App
Open App
Browser
Continue

# Cost to make a string Pangram

Given an array arr[] containing the cost of adding each alphabet from (a – z) and a string str which may or may not be a Pangram. The task is to find the total cost to make the string Pangram.

Examples:

Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26},
str = “abcdefghijklmopqrstuvwz ”
Output : 63
n, x and y are the only characters missing to convert the string into a Pangram.
And their costs are 14, 24 and 25 respectively.
Hence, total cost = 14 + 24 + 25 = 63

Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26},
str = “thequickbrownfoxjumpsoverthelazydog”
Output:
The string is already a Pangram as it contains all the characters from (a – z).

Approach: Mark all the alphabets from (a – z) that occurred in str then find the missing alphabets from the string. Add the cost of these missing alphabets to get the total cost required to make the string Pangram.

Algorithm:

•   Initialize a variable cost to 0 and a boolean array happened with a size of 26, with all entries initialized to false.
•  Mark every instance of an alphabet in the string where it appeared by iterating through the string str and setting the occurred index for each instance to true.
• Iterate through every element of the array arr, and if an index’s value in the occurred boolean array is false, add that index’s value in arr to the cost variable.
• Return the cost variable as the total cost required to make the string Pangram.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the cost to make a string Pangram``#include ``using` `namespace` `std;` `// Function to return the total cost required``// to make the string Pangram``int` `pangramCost(``int` `arr[], string str)``{``    ``int` `cost = 0;``    ``bool` `occurred[26] = { ``false` `};` `    ``// Mark all the alphabets that occurred in the string``    ``for` `(``int` `i = 0; i < str.size(); i++)``        ``occurred[str[i] - ``'a'``] = ``true``;` `    ``// Calculate the total cost for the missing alphabets``    ``for` `(``int` `i = 0; i < 26; i++) {``        ``if` `(!occurred[i])``            ``cost += arr[i];``    ``}` `    ``return` `cost;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,``                  ``16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };``    ``string str = ``"abcdefghijklmopqrstuvwz"``;` `    ``cout << pangramCost(arr, str);``    ``return` `0;``}`

## Java

 `// Java program to find the cost to make a string Pangram` `class` `GFG``{``        ``// Function to return the total cost required``        ``// to make the string Pangram``        ``static`  `int` `pangramCost(``int` `arr[], String str)``        ``{``            ``int` `cost = ``0``;``            ``boolean` `[]occurred=``new` `boolean``[``26``];``            ` `            ``for``(``int` `i=``0``;i<``26``;i++)``             ``occurred[i]=``false``;``        ` `            ``// Mark all the alphabets that occurred in the string``            ``for` `(``int` `i = ``0``; i < str.length(); i++)``                ``occurred[str.charAt(i) - ``'a'``] = ``true``;``        ` `            ``// Calculate the total cost for the missing alphabets``            ``for` `(``int` `i = ``0``; i < ``26``; i++) {``                ``if` `(occurred[i]==``false``)``                    ``cost += arr[i];``            ``}``        ` `            ``return` `cost;``        ``}``        ` `        ``// Driver Code``        ``public` `static` `void` `main(String []args)``        ``{``            ``int` `arr[] = { ``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``10``, ``11``, ``12``, ``13``, ``14``, ``15``,``                        ``16``, ``17``, ``18``, ``19``, ``20``, ``21``, ``22``, ``23``, ``24``, ``25``, ``26` `};``            ``String str = ``"abcdefghijklmopqrstuvwz"``;``        ` `            ``System.out.println(pangramCost(arr, str));``        ` `    ``}``}`  `// This code is contributed by ihritik`

## Python3

 `# Python3 program to find the cost``# to make a string Pangram` `# Function to return the total cost``# required to make the string Pangram``def` `pangramCost(arr, string) :``    ` `    ``cost ``=` `0``    ``occurred ``=` `[``False``] ``*` `26` `    ``# Mark all the alphabets that``    ``# occurred in the string``    ``for` `i ``in` `range``(``len``(string)) :``        ``occurred[``ord``(string[i]) ``-` `ord``(``'a'``)] ``=` `True` `    ``# Calculate the total cost for``    ``# the missing alphabets``    ``for` `i ``in` `range``(``26``) :``        ``if` `(``not` `occurred[i]) :``            ``cost ``+``=` `arr[i]` `    ``return` `cost` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``,``            ``10``, ``11``, ``12``, ``13``, ``14``, ``15``, ``16``,``            ``17``, ``18``, ``19``, ``20``, ``21``, ``22``, ``23``,``            ``24``, ``25``, ``26` `]``    ``string ``=` `"abcdefghijklmopqrstuvwz"` `    ``print``(pangramCost(arr, string))` `# This code is contributed by Ryuga`

## Javascript

 ``

## C#

 `// C# program to find the cost to make a string Pangram` `using` `System;``class` `GFG``{``        ``// Function to return the total cost required``        ``// to make the string Pangram``        ``static`  `int` `pangramCost(``int` `[]arr, ``string` `str)``        ``{``            ``int` `cost = 0;``            ``bool` `[]occurred=``new` `bool``[26];``            ` `            ``for``(``int` `i=0;i<26;i++)``             ``occurred[i]=``false``;``        ` `            ``// Mark all the alphabets that occurred in the string``            ``for` `(``int` `i = 0; i < str.Length; i++)``                ``occurred[str[i] - ``'a'``] = ``true``;``        ` `            ``// Calculate the total cost for the missing alphabets``            ``for` `(``int` `i = 0; i < 26; i++) {``                ``if` `(occurred[i]==``false``)``                    ``cost += arr[i];``            ``}``        ` `            ``return` `cost;``        ``}``        ` `        ``// Driver Code``        ``public` `static` `void` `Main(``string` `[]args)``        ``{``            ``int` `[]arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,``                        ``16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 };``            ``string` `str = ``"abcdefghijklmopqrstuvwz"``;``        ` `            ``Console.WriteLine(pangramCost(arr, str));``        ` `    ``}``}`  `// This code is contributed by ihritik`

## PHP

 ``

Output

`63`

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(26) ⇒ O(1), no extra space is required, so it is a constant.

New Approach:-

Alternative Approach to find the cost to make a string Pangram:

• Instead of using a boolean array to keep track of the occurrence of each alphabet, we can use a HashSet to store the distinct alphabets that appeared in the string. We can then iterate over all the alphabets from ‘a’ to ‘z’, and if an alphabet is not present in the HashSet, we add the corresponding cost from the given array to the total cost.

Algorithm:

1. Initialize a variable cost to 0.
2. Create a HashSet distinct to store the distinct alphabets that appeared in the string.
3. Iterate through the string str, and for each character c, add it to the distinct set.
4. Iterate over all the alphabets from ‘a’ to ‘z’, and if an alphabet is not present in the distinct set, add the corresponding cost from the given array to the total cost.
5. Return the total cost.

Below is the implementation of the above approach:

## C++

 `#include ``using` `namespace` `std;` `// Function to return the total cost required``// to make the string Pangram``int` `pangramCost(``int` `arr[], string str)``{``    ``int` `cost = 0;``    ``unordered_set<``char``> distinct;``    ``// Add all distinct alphabets that appeared in the``    ``// string``    ``for` `(``int` `i = 0; i < str.length(); i++) {``        ``char` `c = str[i];``        ``distinct.insert(c);``    ``}` `    ``// Iterate over all the alphabets from 'a' to 'z',``    ``// and add the corresponding cost for missing``    ``// alphabets``    ``for` `(``char` `c = ``'a'``; c <= ``'z'``; c++) {``        ``if` `(distinct.find(c) == distinct.end()) {``            ``cost += arr;``        ``}``    ``}` `    ``return` `cost;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1,  2,  3,  4,  5,  6,  7,  8,  9,``                  ``10, 11, 12, 13, 14, 15, 16, 17, 18,``                  ``19, 20, 21, 22, 23, 24, 25, 26 };``    ``string str = ``"abcdefghijklmopqrstuvwz"``;` `    ``cout << pangramCost(arr, str) << endl;` `    ``return` `0;``}`

## Java

 `// Java program to find the cost to make a string Pangram` `import` `java.util.HashSet;` `class` `GFG {``    ``// Function to return the total cost required``    ``// to make the string Pangram``    ``static` `int` `pangramCost(``int` `arr[], String str)``    ``{``        ``int` `cost = ``0``;``        ``HashSet distinct``            ``= ``new` `HashSet();``        ``// Add all distinct alphabets that appeared in the``        ``// string``        ``for` `(``int` `i = ``0``; i < str.length(); i++) {``            ``char` `c = str.charAt(i);``            ``distinct.add(c);``        ``}` `        ``// Iterate over all the alphabets from 'a' to 'z',``        ``// and add the corresponding cost for missing``        ``// alphabets``        ``for` `(``char` `c = ``'a'``; c <= ``'z'``; c++) {``            ``if` `(!distinct.contains(c)) {``                ``cost += arr;``            ``}``        ``}` `        ``return` `cost;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = { ``1``,  ``2``,  ``3``,  ``4``,  ``5``,  ``6``,  ``7``,  ``8``,  ``9``,``                      ``10``, ``11``, ``12``, ``13``, ``14``, ``15``, ``16``, ``17``, ``18``,``                      ``19``, ``20``, ``21``, ``22``, ``23``, ``24``, ``25``, ``26` `};``        ``String str = ``"abcdefghijklmopqrstuvwz"``;` `        ``System.out.println(pangramCost(arr, str));``    ``}``}`

## Python3

 `# Python program to find the cost to make a string Pangram` `def` `pangramCost(arr, ``str``):``    ``cost ``=` `0``    ``distinct ``=` `set``()` `    ``# Add all distinct alphabets that appeared in the string``    ``for` `c ``in` `str``:``        ``distinct.add(c)` `    ``# Iterate over all the alphabets from 'a' to 'z',``    ``# and add the corresponding cost for missing alphabets``    ``for` `c ``in` `'abcdefghijklmnopqrstuvwxyz'``:``        ``if` `c ``not` `in` `distinct:``            ``cost ``+``=` `arr[``ord``(c) ``-` `ord``(``'a'``)]` `    ``return` `cost` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5``, ``6``, ``7``, ``8``, ``9``, ``10``, ``11``, ``12``, ``13``, ``14``, ``15``,``           ``16``, ``17``, ``18``, ``19``, ``20``, ``21``, ``22``, ``23``, ``24``, ``25``, ``26``]``    ``str` `=` `"abcdefghijklmopqrstuvwz"` `    ``print``(pangramCost(arr, ``str``))`

## C#

 `using` `System;``using` `System.Collections.Generic;` `class` `GFG``{``  ` `    ``// Function to return the total cost required``    ``// to make the string Pangram``    ``static` `int` `pangramCost(``int``[] arr, ``string` `str)``    ``{``        ``int` `cost = 0;``        ``HashSet<``char``> distinct = ``new` `HashSet<``char``>();``      ` `        ``// Add all distinct alphabets that appeared in the``        ``// string``        ``for` `(``int` `i = 0; i < str.Length; i++) {``            ``char` `c = str[i];``            ``distinct.Add(c);``        ``}` `        ``// Iterate over all the alphabets from 'a' to 'z',``        ``// and add the corresponding cost for missing``        ``// alphabets``        ``for` `(``char` `c = ``'a'``; c <= ``'z'``; c++) {``            ``if` `(!distinct.Contains(c)) {``                ``cost += arr;``            ``}``        ``}` `        ``return` `cost;``    ``}` `    ``// Driver Code``    ``static` `void` `Main(``string``[] args)``    ``{``        ``int``[] arr = { 1,  2,  3,  4,  5,  6,  7,  8,  9,``                      ``10, 11, 12, 13, 14, 15, 16, 17, 18,``                      ``19, 20, 21, 22, 23, 24, 25, 26 };``        ``string` `str = ``"abcdefghijklmopqrstuvwz"``;` `        ``Console.WriteLine(pangramCost(arr, str));``    ``}``}` `// This code is contributed by sarojmcy2e`

Output:-

`63`

Time Complexity: The time complexity of the given program is O(N), where N is the length of the input string. The loop to add distinct characters in HashSet takes O(N) time, and the loop to iterate over all the alphabets takes O(26) time, which is a constant value. Thus, the overall time complexity is O(N + 26), which can be simplified to O(N).

Auxiliary Space: The auxiliary space used by the given program is O(1), which is constant space. The space used by HashSet is proportional to the number of distinct characters in the string, which in this case is at most 26 (the number of alphabets in the English language). Therefore, the overall space complexity is O(26), which is constant space.

My Personal Notes arrow_drop_up