Cost to make a string Panagram

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

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: 0
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.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the cost to make a string Panagram
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the cost to make a string Panagram
  
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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the cost 
# to make a string Panagram 
  
# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the cost to make a string Panagram
  
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

chevron_right


PHP

Output:

63


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : AnkitRai01, ihritik



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.