Cost of expensive string
Problem Statement
Implement the
following function:
static int MaxCost(String
str1, String str2) {}
The function accepts two strings 'str1' and
'str2' of length 'm' and 'n' respectively as its argument. Cost of a string is
equal to the sum of cost of all characters in that string. Cost of a character
is equal to its position in alphabetical series (i.e. cost of a=1, b=2, c=3 and
so on till z= 26). Implement the function to find the cost of both the strings
and return the maximum cost.
Assumption: String contains only lower-case alphabets.
Note:
·
Computed
values lie within integral range.
·
Return
-1 if both the strings are null.
·
If
only one of the string is null, return the cost of other string.
·
In
case of python, null refers to None.
Example:
Input:
str1: head
str2: express
Output:
106
Explanation:
Cost of str1 =
8+5+1+4 = 18
Cost of str2 =
5+24+16+18+5+19+19 = 106
Since, (18<
106) thus, output is 106.
The custom input
format for the above case:
4
7
head
express
(The first line
represents the length of 'str1', the second line represents the length of
'str2', the third line represents 'str1', the fourth line represents 'str2').
Sample Input
str1: say
str2: be
Sample Output
45
The custom input
format for the above case:
3
2
say
be
(The first line
represents the length of 'str1', the second line represents the length of
'str2', the third line represents 'str1', the fourth line represents 'str2').
Instructions:
·
This
is a template based question, DO NOT write the "main" function.
·
Your
code is judged by an automated system, do not write any additional
welcome/greeting messages.
·
"Save
and Test" only checks for basic test cases, more rigorous cases will be
used to judge your code while scoring.
No comments:
Post a Comment