Dada una array arr[] que contiene el costo de agregar cada alfabeto de (a – z) y una string str que puede o no ser un Panagram . La tarea es encontrar el costo total para hacer el Panagrama de cuerdas.
Ejemplos:
Entrada: 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 ”
Salida: 63
n, xey son los únicos caracteres que faltan para convertir la string en un pangrama.
Y sus costes son 14, 24 y 25 respectivamente.
Por lo tanto, costo total = 14 + 24 + 25 = 63Entrada: 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”
Salida: 0
La string ya es un Pangram ya que contiene todos los caracteres de (a – z).
Enfoque: marque todos los alfabetos desde (a – z) que ocurrieron en str y luego encuentre los alfabetos que faltan en la string. Sume el costo de estos alfabetos que faltan para obtener el costo total requerido para hacer el pangrama de cuerdas.
A continuación se muestra la implementación del enfoque anterior:
C++
// 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; }
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
<?php // PHP program to find the cost to make // a string Panagram // Function to return the total cost // required to make the string Pangram function pangramCost($arr, $str) { $cost = 0; $occurred = array(); for($i = 0; $i < 26; $i++) $occurred[$i] = false; // Mark all the alphabets that occurred // in the string for($i = 0; $i < strlen($str); $i++) { $idx = ord($str[$i]) - 97; $occurred[$idx] = true; } // Calculate the total cost for the // missing alphabets for($i = 0; $i < 26; $i++) { if ($occurred[$i] == false) $cost += $arr[$i]; } return $cost; } // Driver Code $arr = array( 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"; echo pangramCost($arr, $str); // This code is contributed by ihritik ?>
Javascript
<script> // Javascript program to find the cost to // make a string Panagram // Function to return the total cost required // to make the string Pangram function pangramCost(arr, str) { var cost = 0; var occurred = new Array(26); for(let i = 0; i < 26; i++) occurred[i] = false; // Mark all the alphabets that occurred // in the string for(let i = 0; i < str.length; i++) occurred[str[i].charCodeAt() - 'a'.charCodeAt()] = true; // Calculate the total cost for // the missing alphabets for(let i = 0; i < 26; i++) { if (occurred[i] == false) cost += arr[i]; } return cost; } // Driver Code var 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 ]; var str = "abcdefghijklmopqrstuvwz"; document.write(pangramCost(arr, str)); // This code is contributed by bunnyram19 </script>
63
Publicación traducida automáticamente
Artículo escrito por SURENDRA_GANGWAR y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA