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]

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]