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: 1
💡 Note: For Alice's sequence FW, Bob can win with sequence WE: (W vs F: Bob+1), (E vs W: Bob+1), final score 2-0.
Example 2 — Single Round
$ Input: s = "F"
Output: 1
💡 Note: Bob can win only by playing W (Water beats Fire), giving him 1 point vs Alice's 0.
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
Count The Number of Winning Sequences INPUT Alice's Sequence: s = "FW" F Fire Dragon W Water Serpent Battle Rules: F beats E W beats F E beats W Constraints: n = 2 rounds Bob: no same creature in consecutive rounds ALGORITHM STEPS 1 Define DP State dp[i][last][diff] = ways 2 Track Score Diff diff = Bob pts - Alice pts 3 Transition Rules Bob picks F, W, or E 4 Count Winning Sum all diff > 0 Bob's Valid Sequences: Round 1 | Round 2 | Result W(+1) | F(-1) | diff=0 W(+1) | E(+1) | diff=2 OK E(0) | F(-1) | diff=-1 E(0) | W(0) | diff=0 F(-1) | E(+1) | diff=0 F(-1) | W(0) | diff=-1 FINAL RESULT Winning Sequences for Bob: WE: W beats F(+1), E beats W(+1) diff = 2 > 0 -- Bob Wins! EF: tie(0), F loses to W(-1) diff = -1 -- Bob Loses Output: 4 Verification: 6 total sequences possible (no consecutive same creature) 4 sequences have diff > 0 Key Insight: Use Dynamic Programming with states: (round index, Bob's last creature, score difference). The constraint that Bob cannot repeat creatures limits transitions. Track point differential instead of separate scores. Count sequences where final diff > 0. Use modulo 10^9+7. TutorialsPoint - Count The Number of Winning Sequences | Optimal Solution
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