Problem of the day
Ninja and his girlfriend were getting married. Ninja’s father invited the ninja and his girlfriend to give blessings. Father has 2 blessings. Each blessing is in the form of a string consisting of lowercase characters(a-z) only.
But he can give only one blessing of ‘K’ length because some priest told him to do so. Thus he decides to generate a blessing using the other two blessings. While doing this, he wants to ensure that happiness brought into their life by his blessing is maximum.
The generated blessing is a common subsequence of length ‘K’ of the two blessings he has.
Happiness of the blessing he generates is calculated by the sum of ASCII values of characters in the blessing, and he wants the happiness to be maximum.
If he is not able to generate a common subsequence of length K, then the happiness is 0 (zero).
Ninja’s father comes to you and asks you to find the maximum happiness that can be generated by the two blessings he has.
Example: lInput: ‘BLESSING1’ = “asdf” ‘BLESSING2’ = “asdf” ‘K’ = 3
Output: 317
The optimal strategy would be
To have the blessing as “sdf” which results in the total happiness of 115+100+102 = 317.
The first line consists of a number of test cases ‘T’.
The first line of each test case contains the string ‘BLESSING1’.
The second line of each test case contains the string ‘BLESSING2’.
The third line of each test case contains the length of common Subsequence 'K'.
Output format :
The output consists of ‘T’ lines, each containing an integer that denotes the maximum happiness value the two blessings can generate.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= length(BLESSING1), length(BLESSING2) <= 100
Time Limit: 1 sec
2
asdf
asdf
3
anandi
jagya
3
317
0
For the first test case:-
The optimal strategy would be
To have the blessing as “sdf” which results in the total happiness of 115+100+102 = 317.
For the second test case:-
No common subsequence of length 3 is possible hence, the result is 0.
2
axyz
yaxz
2
aaaaaazzzz
zzzzaaaaaa
4
243
488
Think of trying out all possible common subsequences of length K and choose the one which has the maximum happiness.
In this approach, we will recursively try out all possible common subsequences of length K. A recursive function is called for every combination of the ending index of the two strings and the length of the common subsequence. Let’s say a common subsequence is ending with the ‘i’ index in ‘BLESSING1’ and with the ‘j’ index in ‘BLESSING2’ and the length of the required common subsequence is ‘k’.
Our function returns the max value for a subsequence of length ‘k’ and ‘i’ and ‘j’ are a current index to ‘BLESSING1’ and ‘BLESSING2’. Thus we have 3 state variables. Now all you need to do is find the maximum value among all combinations of possible ‘i’, ‘j’, ’k’;
The steps are as follows:-
// Recursive function to find maximum happiness
function getMaxHappinessUtil ( String BLESSING1, String BLESSING2, int i,
int j, int K ):
function getMaxHappiness(string BLESSING1, string BLESSING2, int N, int M,int K ):
O( 3M+N ), Where ‘N’ is the length of string ‘BLESSING1’ and ‘M’ is the length of the string ‘BLESSING2’.
In the worst case recursion function will call 3 other recursive function each of which calls 3 other ones and it goes on and on till the sum of the length of both strings.
O( N + M ), Where ‘N’ is the length of string ‘BLESSING1’ and ‘M’ is the length of the string ‘BLESSING2’.
Extra auxiliary space will be needed for the recursion call stack.
Hence space complexity is O( N + M ).