Ctrl K

Element Duel

Hard
Two mages fight a duel of n rounds. In every round both of them summon one of three elements at the same time: fire (F), water (W) or earth (E). The round's point goes to whoever summoned the stronger element: - fire beats earth; - water beats fire; - earth beats water. If both pick the same element, nobody scores. You know your rival's plan in advance: the string rival, where rival[i] is their element in round i. You make your own plan, but you may never summon the same element in two consecutive rounds. Count how many of your plans leave you with strictly more points than your rival after the last round. The answer can be very large, so return it modulo 10⁹ + 7.
Example 1
rival="W" → 1

Only earth beats water, so the single winning plan is "E".

Example 2
rival="EF" → 2

There are six allowed plans. Two of them win: "FW" (fire beats earth, water beats fire, 2 : 0) and "EW" (a draw, then water beats fire, 1 : 0).

Example 3
rival="FFFF" → 8
Constraints
  • 1 ≤ len(rival) ≤ 1000
  • rival consists only of the characters F, W and E
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPattern With Dot and Star HardLongest Balanced Bracket Stretch HardDoes the Text Fit the Pattern HardShuffle by Cutting and Swapping HardWays to Cross Out Hard
loading Python…
Input
rival="W"
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 33 tests on the server, hidden ones included.