LeetCode 3393: Count Paths With the Given XOR Value Solution

Master LeetCode problem 3393 (Count Paths With the Given XOR Value), a medium 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.

3393. Count Paths With the Given XOR Value

Problem Explanation

Explanation

To solve this problem, we can use dynamic programming to keep track of the number of paths that lead to a cell with a specific XOR value. We will create a 2D DP array to store the count of paths with the given XOR value up to that cell. We will iterate through the grid and update the DP array accordingly.

  1. Initialize a 2D DP array dp where dp[i][j][xor] represents the number of paths to reach cell (i, j) with an XOR value of xor.
  2. Initialize dp[0][0][grid[0][0]] = 1 since there is one way to reach the starting cell with an XOR value of grid[0][0].
  3. Iterate through the grid from top-left to bottom-right:
    • For each cell (i, j), update the dp array based on the XOR values:
      • dp[i][j][xor] += dp[i-1][j][xor ^ grid[i][j]] if i > 0 (from top cell)
      • dp[i][j][xor] += dp[i][j-1][xor ^ grid[i][j]] if j > 0 (from left cell)
  4. The result will be dp[m-1][n-1][k] representing the number of paths from top-left to bottom-right with an XOR value of k.

Time complexity: O(m * n * k) Space complexity: O(m * n * k)

Solution Code

class Solution {
    public int countPaths(int[][] grid, int k) {
        int m = grid.length;
        int n = grid[0].length;
        int MOD = 1000000007;
        int[][][] dp = new int[m][n][16];
        dp[0][0][grid[0][0]] = 1;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                for (int xor = 0; xor < 16; xor++) {
                    if (i > 0) {
                        dp[i][j][xor] += dp[i - 1][j][xor ^ grid[i][j]];
                        dp[i][j][xor] %= MOD;
                    }
                    if (j > 0) {
                        dp[i][j][xor] += dp[i][j - 1][xor ^ grid[i][j]];
                        dp[i][j][xor] %= MOD;
                    }
                }
            }
        }

        return dp[m - 1][n - 1][k];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3393 (Count Paths With the Given XOR Value)?

This page provides optimized solutions for LeetCode problem 3393 (Count Paths With the Given XOR Value) 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 3393 (Count Paths With the Given XOR Value)?

The time complexity for LeetCode 3393 (Count Paths With the Given XOR Value) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3393 on DevExCode?

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

Back to LeetCode Solutions