HackerEarth - Molly And Flame Solution
1 min read

HackerEarth - Molly And Flame Solution

HackerEarth - Molly And Flame Solution

Molly is in love with Sherlock. She wants to decide the nature of the relationship between her and Sherlock using the game of FLAME.

FLAME stands for:

The rules of FLAME are:

1) The names of the two people are taken.

2) Then the common letters are deleted from both the names.

3) Then x_rem is calculated which is the number of leftover letters in both the names.

4) Count through the letters of “FLAME” x_rem times. After you have counted the 5 letters of “FLAME,” you can continue counting from 6 to 10 starting again at “F” and ending at “E,” if needed. If you have more than 10 leftover letters, repeat the process a third time, starting with “F.”

5) The letter at which you stop decides the nature of the relationship.


1)consider the strings sherlock and molly.

2)After striking off the common letters: sherck my

3) x_rem = len(sherck) + len(my) = 8

4) counting through the letters of FLAME.


5) The count stops at A. Hence the result is Affection.

However, Molly is not happy with the result of the game(Affection) and she manipulates the string FLAME TO FALME to change the result of the game(Love).

Given two names perform a minimum number of swaps on the string “FLAME” and print the new string so that the nature of the relationship is always Love.

Input Format:
First-line containing a number of test cases t. Next t lines contain two space-separated names in lowercase.
Output Format:
Print the new string in a new line for each test case.


  • 1≤T≤100
  • 1≤lengthofname≤100000


sherlock molly
watson mary



Solution in Python

t = int(input())
for _ in range(t):
    a,b = input().strip().split()
    c = set(a) & set(b)
    l = (sum(1 for i in a+b if i not in c)%5 or 5)-1
    s = list("FLAME")    
    s[l],s[1] = s[1],s[l]

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.