📜  制作字符串Panagram 的成本

📅  最后修改于: 2022-05-13 01:57:08.612000             🧑  作者: Mango

制作字符串Panagram 的成本

给定一个数组arr[] ,其中包含从(a – z)中添加每个字母的成本和一个字符串str ,它可能是也可能不是 Panagram。任务是找出制作字符串Panagram 的总成本。

例子:

方法:标记出现在str中的(a – z)中的所有字母,然后从字符串中找到缺失的字母。添加这些缺失字母的成本以获得制作字符串Pangram 所需的总成本。

下面是上述方法的实现:

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


Python3
# 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


C#
// 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


PHP


Javascript


输出:
63