Can anyone share his approach for hackerrank hackfest last question

link : https://www.hackerrank.com/contests/hackerrank-hackfest-2020/challenges/rbg