Count The Number of Winning Sequences - Problem

In this fantasy battle game, Alice and Bob face off in n rounds of magical creature summoning! Each round, they simultaneously choose one of three powerful creatures:

  • ๐Ÿ”ฅ Fire Dragon ('F') - Burns Earth Golems to ash
  • ๐ŸŒŠ Water Serpent ('W') - Extinguishes Fire Dragons
  • ๐ŸŒ Earth Golem ('E') - Absorbs Water Serpents

The rock-paper-scissors style scoring works as follows:

  • Fire Dragon beats Earth Golem (Fire burns Earth)
  • Water Serpent beats Fire Dragon (Water extinguishes Fire)
  • Earth Golem beats Water Serpent (Earth absorbs Water)
  • Same creatures = No points for either player

You know Alice's complete strategy as a string s of length n, but Bob's moves are unknown. However, Bob has one constraint: he never summons the same creature twice in a row.

Goal: Count how many different sequences Bob can play to strictly beat Alice (score more points than her). Return the answer modulo 10^9 + 7.

Input & Output

example_1.py โ€” Basic Example
$ Input: s = "FFF"
โ€บ Output: 3
๐Ÿ’ก Note: Alice plays F-F-F. Bob can win with: WWW (+3), WWE (+1), WEW (+1). Bob cannot repeat creatures consecutively, so these are the only winning sequences.
example_2.py โ€” Mixed Strategy
$ Input: s = "FWEFW"
โ€บ Output: 18
๐Ÿ’ก Note: Alice plays a mixed strategy. Bob has multiple ways to counter each move while respecting the no-consecutive-same-creature constraint.
example_3.py โ€” Single Round
$ Input: s = "E"
โ€บ Output: 1
๐Ÿ’ก Note: Alice plays Earth Golem. Bob can only win by playing Water Serpent (W), since E beats W is false, but W beats F and F beats E, so W vs E gives 0 points. Actually, Bob wins with F (Fire beats Earth).

Visualization

Tap to expand
Fantasy Battle Game: Bob's Winning Strategies๐Ÿ”ฅ FIREDRAGONBeats Earth๐ŸŒŠ WATERSERPENTBeats Fire๐ŸŒ EARTHGOLEMBeats WaterAlice's Strategy: KnownExample: s = "FWE"Bob's ConstraintCannot summon samecreature consecutivelyDP Solution: O(nยฒ)State = (round, last_creature, score_diff)Memoize to avoid recalculation๐ŸŽฏ Result: Count all valid Bob sequences where his score > Alice'sEfficiently handles up to 1000 rounds using dynamic programming
Understanding the Visualization
1
Know the Rules
Fire beats Earth, Water beats Fire, Earth beats Water (like rock-paper-scissors)
2
Constraint
Bob cannot summon the same creature twice in a row
3
Dynamic Programming
Track all possible game states: (round, last_creature, score_difference)
4
Count Winners
Sum up all paths where Bob's final score > Alice's score
Key Takeaway
๐ŸŽฏ Key Insight: Use dynamic programming to track game states (round, last_creature, score_difference) with memoization, avoiding the exponential time complexity of brute force while systematically counting all winning sequences for Bob.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

n rounds ร— 3 creatures ร— O(n) possible score differences, with memoization preventing recalculation

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

Memoization table storing results for all possible states

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 1000
  • s consists only of characters 'F', 'W', and 'E'
  • Bob never summons the same creature in consecutive rounds
  • Answer fits in 32-bit integer when taken modulo 109 + 7
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
43.7K Views
Medium-High Frequency
~35 min Avg. Time
1.8K 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