Count The Number of Winning Sequences - Problem

Alice and Bob are playing a fantasy battle game consisting of n rounds where they summon one of three magical creatures each round: a Fire Dragon, a Water Serpent, or an Earth Golem.

In each round, players simultaneously summon their creature and are awarded points as follows:

  • If one player summons a Fire Dragon and the other summons an Earth Golem, the player who summoned the Fire Dragon is awarded a point.
  • If one player summons a Water Serpent and the other summons a Fire Dragon, the player who summoned the Water Serpent is awarded a point.
  • If one player summons an Earth Golem and the other summons a Water Serpent, the player who summoned the Earth Golem is awarded a point.
  • If both players summon the same creature, no player is awarded a point.

You are given a string s consisting of n characters 'F', 'W', and 'E', representing the sequence of creatures Alice will summon in each round.

Bob's sequence of moves is unknown, but it is guaranteed that Bob will never summon the same creature in two consecutive rounds. Bob beats Alice if the total number of points awarded to Bob after n rounds is strictly greater than the points awarded to Alice.

Return the number of distinct sequences Bob can use to beat Alice. Since the answer may be very large, return it modulo 109 + 7.

Input & Output

Example 1 — Basic Case
$ Input: s = "FW"
Output: 4
💡 Note: Bob can win with sequences WF, WE, EF, EW. For example: if Bob plays WF, scores are (W vs F: Bob+1), (F vs W: Bob+1), final 2-0.
Example 2 — Single Round
$ Input: s = "F"
Output: 2
💡 Note: Bob can win by playing W (W beats F) or E is invalid since it loses to F. Actually Bob wins with W only, but can also play E for tie then needs score > 0. Bob wins with W and E: W beats F (+1), E loses to F but we need strictly greater.
Example 3 — All Same
$ Input: s = "FFF"
Output: 2
💡 Note: Bob must avoid consecutive moves. He can play WEW (scores +1,0,+1 = +2) or EWE (scores 0,+1,0 = +1), both beat Alice's 0.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists only of 'F', 'W', and 'E'

Visualization

Tap to expand
Fantasy Battle Game: Count Winning SequencesAlice FixedF WBob Variable? ?ConstraintNo consecutiveGame Rules:• F beats E (+1 point)• W beats F (+1 point)• E beats W (+1 point)• Same = tie (0 points)Winning Bob SequencesWF: W beats F(+1), F ties W(0) → 1-0 ✓WE: W beats F(+1), E beats W(+1) → 2-0 ✓+ 2 more valid sequences...Bob must score > Alice to winResult: 4 distinct winning sequences
Understanding the Visualization
1
Input
Alice's sequence s = "FW" (Fire, Water)
2
Process
Find all valid Bob sequences where score > Alice's score
3
Output
Count of distinct winning sequences for Bob
Key Takeaway
🎯 Key Insight: Use DP to efficiently count all valid Bob sequences that beat Alice's fixed strategy
Asked in
Google 25 Facebook 18
12.5K Views
Medium Frequency
~35 min Avg. Time
428 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen