LeetCode 3320: Count The Number of Winning Sequences Solution

Master LeetCode problem 3320 (Count The Number of Winning Sequences), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3320. Count The Number of Winning Sequences

Problem Explanation

Explanation

To solve this problem, we can use dynamic programming to keep track of the number of winning sequences Bob can use to beat Alice for each possible ending move Bob makes in the current round. We initialize a 3D dynamic programming array dp where dp[i][j][k] represents the number of winning sequences Bob can use to beat Alice after i rounds, where the last move Bob made was creature j and the last move Alice made was creature k.

At each round, we iterate over all possible moves for Bob and Alice and update the dp array based on the rules mentioned in the problem statement. Finally, we sum up all winning sequences for Bob with different ending moves and return the total count modulo 10^9 + 7.

Time Complexity

The time complexity of this solution is O(n) where n is the length of the input string s.

Space Complexity

The space complexity of this solution is O(n) to store the 3D dynamic programming array.

Solution Code

class Solution {
    public int countTheNumberOfWinningSequences(String s) {
        int mod = 1000000007;
        int n = s.length();
        long[][][] dp = new long[n + 1][3][3];
        
        for (int k = 0; k < 3; k++) {
            dp[1][k][k] = 1;
        }
        
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 3; k++) {
                    for (int l = 0; l < 3; l++) {
                        if ((j != k) || (j == l)) {
                            dp[i][j][k] = (dp[i][j][k] + dp[i - 1][l][j]) % mod;
                        }
                    }
                }
            }
        }
        
        long count = 0;
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 3; k++) {
                count = (count + dp[n][j][k]) % mod;
            }
        }
        
        return (int) count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3320 (Count The Number of Winning Sequences)?

This page provides optimized solutions for LeetCode problem 3320 (Count The Number of Winning Sequences) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3320 (Count The Number of Winning Sequences)?

The time complexity for LeetCode 3320 (Count The Number of Winning Sequences) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3320 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3320 in Java, C++, or Python.

Back to LeetCode Solutions