Cost to make a string Panagram | Set 2
Given an array cost[] containing the cost of adding each alphabet from (a – z) and a string str consisting of lowercase English alphabets which may or may not be a Panagram. The task is to make the given string a Panagram with the following operations:
- Adding a character in str costs twice the cost associated with that character.
- Removing a character from str will result in gaining the exact cost associated with that character.
Print the cost of making the given string a Panagram, if the gain is more than the cost then print 0.
Examples:
Input: arr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
str = “geeksforgeeks”
Output: 32
Occurrences of the characters are as follow:
g = 2, e = 4, k = 2, s = 2, f = 1, o = 1 and r = 1
The remaining 19 characters from the English alphabets will be added at cost 2 for each i.e. 2 * 19 = 38
And, 1, 3, 1 and 1 occurrences of the characters ‘g’, ‘e’, ‘k’ and ‘s’ respectively can be traded for gain (1 + 3 + 1 + 1) i.e. 6
So, the normalized cost is 38 – 6 = 32
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 given string is already a Panagram
Approach:
- Store the occurrence for each of the character in an array occurrences[].
- Initialize gain = 0 and start traversing the array occurrences[], for every character ch:
- If ch occurs more than once say x times then x – 1 of its occurrences can be traded for some gain i.e. gain = gain + cost[ch] * (x – 1).
- If ch doesn’t occur in str then it has to be added at twice the cost i.e. gain = gain – (2 * cost[ch]).
- If gain ? 0 then print 0.
- Else print gain * -1 which is the cost of making str a Panagram.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int costToPanagram(string str, int cost[])
{
int i, n = str.length();
int occurrences[26] = {0};
for (i = 0; i < n; i++)
occurrences[str[i] - 'a' ]++;
int gain = 0;
for (i = 0; i < 26; i++)
{
if (occurrences[i] == 0)
gain -= (2 * cost[i]);
else if (occurrences[i] > 1)
gain += (cost[i] * (occurrences[i] - 1));
}
if (gain >= 0)
return 0;
return (gain * -1);
}
int main()
{
int cost[] = { 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
string str = "geeksforgeeks" ;
cout << costToPanagram(str, cost);
}
|
Java
public class GFG {
static int costToPanagram(String str, int cost[])
{
int i, n = str.length();
int occurrences[] = new int [ 26 ];
for (i = 0 ; i < n; i++)
occurrences[str.charAt(i) - 'a' ]++;
int gain = 0 ;
for (i = 0 ; i < 26 ; i++) {
if (occurrences[i] == 0 )
gain -= ( 2 * cost[i]);
else if (occurrences[i] > 1 )
gain += (cost[i] * (occurrences[i] - 1 ));
}
if (gain >= 0 )
return 0 ;
return (gain * - 1 );
}
public static void main(String[] args)
{
int cost[] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
String str = "geeksforgeeks" ;
System.out.println(costToPanagram(str, cost));
}
}
|
Python3
def costToPanagram(string, cost):
n = len (string)
occurrences = [ 0 ] * 26
for i in range (n):
occurrences[ ord (string[i]) - ord ( 'a' )] + = 1
gain = 0
for i in range ( 26 ):
if occurrences[i] = = 0 :
gain - = 2 * cost[i]
elif occurrences[i] > 1 :
gain + = cost[i] * (occurrences[i] - 1 )
if gain > = 0 :
return 0
return gain * - 1
if __name__ = = "__main__" :
cost = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 ]
string = "geeksforgeeks"
print (costToPanagram(string, cost))
|
C#
using System;
class GFG
{
static int costToPanagram( string str, int []cost)
{
int i, n = str.Length ;
int []occurrences = new int [26];
for (i = 0; i < n; i++)
occurrences[str[i] - 'a' ]++;
int gain = 0;
for (i = 0; i < 26; i++)
{
if (occurrences[i] == 0)
gain -= (2 * cost[i]);
else if (occurrences[i] > 1)
gain += (cost[i] * (occurrences[i] - 1));
}
if (gain >= 0)
return 0;
return (gain * -1);
}
public static void Main()
{
int []cost = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
string str = "geeksforgeeks" ;
Console.WriteLine(costToPanagram(str, cost));
}
}
|
Javascript
<script>
function costToPanagram(str, cost)
{
var i, n = str.length;
var occurrences = Array(26).fill(0);
for (i = 0; i < n; i++)
occurrences[str[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
var gain = 0;
for (i = 0; i < 26; i++)
{
if (occurrences[i] == 0)
gain -= (2 * cost[i]);
else if (occurrences[i] > 1)
gain += (cost[i] * (occurrences[i] - 1));
}
if (gain >= 0)
return 0;
return (gain * -1);
}
var cost = [1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
var str = "geeksforgeeks" ;
document.write( costToPanagram(str, cost));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)
Last Updated :
12 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...