Question:
Neat String
A neat string is one in which for a particular alphabet (from ‘a’ to ‘z’), the string should contain only its uppercase characters or lowercase characters.
For example, the neat strings for “aAbB” are “aabb”, “AAbb”, “aaBB”, “AABB”.
You are given a string S with size N. The string contains both lowercase and uppercase characters. You can perform one of the following operations on any character of the string any number of times, where each operation has a cost associated with it.
-
Change a character from lowercase to uppercase. Every such operation incurs a cost CU. For eg - changing “yyyy” to “Yyyy” would cost 1 CU.
-
Change a character from uppercase to lowercase. Every such operation incurs a cost CL. For eg - changing “Aaa” to “aaa” would cost 1 CL.
Your task is to convert the given string into a neat string which requires the minimum total cost to build.
Note: If there are multiple neat strings with the same minimum total cost to build, the one among them with the maximum uppercase characters has to be selected.
Input Specification:
input1: a string S
input2: an integer denoting the length of string S
input3: an integer denoting the cost CL i.e., the cost of changing a character to lowercase
input4: an integer denoting the cost CU i.e., the cost of changing a character to uppercase
Output Specification:
Return the Neat string that requires the minimum total cost to build.
Example 1:
input1: aabbAA
input2: 6
input3: 1
input4: 1
Output: AAbbAA
Explanation:
There are 2 Neat strings possible for “aabbAA” with a minimum total cost which are “aabbaa” and “AAbbAA”
The total cost for converting “aabbAA” to “AAbbAA” is 2 as:
Converting “aabbAA” to “aAbbAA” costs 1 CU and,
Further converting “aAbbAA” to “AAbbAA” costs 1 CU so Total cost = 2
And, the total cost for converting “aabbAA” to “aabbaa” is 2 as :
Converting “aabbAA” to “aabbaA” costs 1 CU and,
Further converting “aabbaA” to “aabbaa” costs 1 CU so Total cost = 2
We need to return “AAbbAA” because it has more uppercase characters than “aabbaa”
Example 2:
input1: aAPbBP
input2: 6
input3: 1
input4: 2
Output: aaPbbP
Explanation:
There are 4 Neat strings possible for “aAPbBP” which are - “aaPbbP”, “AAPBBP”, “AAPbbP” and “aaPBBP”.
The answer is “aaPbbP” as it requires a minimum total cost to build which is 2.
“aAPbBP” to “aaPbBP” costs 1 CL
"aaPbBP to “aaPbbP” costs 1 CL, Total cost = 1+1 = 2