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++
// 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 ?> |
63
Recommended Posts:
- Cost to make a string Panagram | Set 2
- Minimum cost to make a string free of a subsequence
- Python set to check if string is panagram
- Minimum cost to make two strings same
- Minimum Cost To Make Two Strings Identical
- Minimum Cost to make two Numeric Strings Identical
- Minimum cost to make two strings identical by deleting the digits
- Minimum cost to construct a string
- Minimum cost to modify a string
- Minimum cost to convert string into palindrome
- Minimum changes required to make first string substring of second string
- Check if a string can be repeated to make another string
- Minimum changes to a string to make all substrings distinct
- Remove a character from a string to make it a palindrome
- Missing characters to make a string Pangram
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.



