Approach

I’m thinking that this is a dynamic programming problem, and I use the dfs with memo, but I then find out that the key is idx, maxNum, do1, which is a really large number, and I implemet with a map<pair<int,int>,int> first, but get an TLE.

I then change the map<pair<int,int>,int> to unordered_map<long long, int>, getting a pass but still really slow.

After reading others’ solution, there is a better way to do this, we maintain the current xor sum cur, and record how many solutions ending with pairing target1 as mp1 (ends with target2 as mp2). When approahcing curent xor sum, if we want it to end with target1, we find if there are solutions that ending with target1 ^ cur, and if there is an anwser, we update mp1(do this also fortarget2andmp2`).

Code-1: DFS with memorization (slow)

class Solution {
public:
    const int mod = 1'000'000'007;
    
    int alternatingXOR(vector<int>& nums, int target1, int target2) {
        int cur = 0;
        return traverse(nums, target1, target2, 0, cur, true);
    }

    int traverse(const vector<int>& nums, const int& target1, const int& target2, int idx, int cur, bool do1) {
        cur ^= nums[idx];
        long long count = 0;
        if(idx == nums.size()-1) {
            if(do1 && cur == target1) return 1;
            if(!do1 && cur == target2) return 1;
            return 0;
        }
        // lg2 100000 = 18
        long long key = (((long long)idx) << 20) + (((long long)cur) << 1) + (do1 ? 1 : 0);
        if(memo.count(key)) return memo[key];
        if(do1) {
            count += traverse(nums, target1, target2, idx+1, cur, true);
            count %= mod;
            if(cur == target1) {
                count += traverse(nums, target1, target2, idx+1, 0, false);
                count %= mod;
            }
        } else {
            count += traverse(nums, target1, target2, idx+1, cur, false);
            count %= mod;
            if(cur == target2) {
                count += traverse(nums, target1, target2, idx+1, 0, true);
                count %= mod;
            }
        }
        // memo
        memo[key] = count;
        return count;
    }

    unordered_map<long long, int> memo;
};

Code-2: DP for ending with target1 and targe2

class Solution {
public:
    const int mod = 1'000'000'007;

    int alternatingXOR(vector<int>& nums, int target1, int target2) {
        int n = nums.size();
        int cur = 0;
        unordered_map<int, long long> mp1;
        unordered_map<int, long long> mp2;
        mp2[0] = 1;
        for(int i = 0 ; i < n ; ++i) {
            cur ^= nums[i];
            // target 1 end here
            int rem2 = cur ^ target1; 
            int count2 = mp2.count(rem2) ? mp2[rem2] : 0;
            // take targe2
            int rem1 = cur ^ target2;
            int count1 = mp1.count(rem1) ? mp1[rem1] : 0;

            if(count2 != 0) mp1[cur] = (mp1[cur] + count2) % mod;
            if(count1 != 0) mp2[cur] = (mp2[cur] + count1) % mod;
            
            if(i == n-1) return (count1 + count2) % mod;
        }
        return 0;
    }
};