New update is available. Click here to update.
Close
Topic list
NINJA AND HAPPINESS
HARD
45 mins
Dynamic Programming
Topics (Covered in this problem)
Problem solved
Skill meter
Dynamic Programming
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# NINJA AND HAPPINESS

Contributed by
Abhishek Gupta
Hard
0/120
Avg time to solve 45 mins
Success Rate 40 %
Share

## Problem Statement

#### 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: l
``````Input: ‘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.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1 :
``````2
asdf
asdf
3
anandi
jagya
3
``````
##### Sample Output 1 :
``````317
0
``````
##### Explanation Of Sample Input 1 :
``````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.
``````
##### Sample Input 2 :
``````2
axyz
yaxz
2
aaaaaazzzz
zzzzaaaaaa
4
``````
##### Sample Output 2 :
``````243
488
``````
Auto
Console