/**
* Problem: LeetCode 3514 - Number of Unique XOR Triplets II
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int uniqueXorTriplets(int[] nums) {
final int MAX = 2048;
boolean[][] dp = new boolean[4][MAX];
dp[0][0] = true;
for (int x : nums) {
boolean[][] next = new boolean[4][MAX];
for (int k = 0; k <= 3; k++) {
System.arraycopy(dp[k], 0, next[k], 0, MAX);
}
// Use the current index up to 3 times (i <= j <= k allows repeats)
for (int t = 0; t < 3; t++) {
for (int k = 2; k >= 0; k--) {
for (int v = 0; v < MAX; v++) {
if (next[k][v]) {
next[k + 1][v ^ x] = true;
}
}
}
}
dp = next;
}
int ans = 0;
for (boolean ok : dp[3]) {
if (ok) ans++;
}
return ans;
}
}
"""
Problem: LeetCode 3514 - Number of Unique XOR Triplets II
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def uniqueXorTriplets(self, nums: List[int]) -> int:
dp = [set() for _ in range(4)]
dp[0].add(0)
for x in nums:
next_dp = [set(s) for s in dp]
# Use current element up to 3 times (i <= j <= k allows repeats)
for _ in range(3):
for k in range(2, -1, -1):
for v in list(next_dp[k]):
next_dp[k + 1].add(v ^ x)
dp = next_dp
return len(dp[3])
/**
* Problem: LeetCode 3514 - Number of Unique XOR Triplets II
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <cstring>
/**
* Problem: LeetCode 3514 - Number of Unique XOR Triplets II
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int uniqueXorTriplets(std::vector<int>& nums) {
const int MAX = 2048;
bool dp[4][MAX];
std::memset(dp, 0, sizeof(dp));
dp[0][0] = true;
for (int x : nums) {
bool next[4][MAX];
std::memcpy(next, dp, sizeof(dp));
// Use current element up to 3 times (i <= j <= k allows repeats)
for (int t = 0; t < 3; t++) {
for (int k = 2; k >= 0; k--) {
for (int v = 0; v < MAX; v++) {
if (next[k][v]) {
next[k + 1][v ^ x] = true;
}
}
}
}
std::memcpy(dp, next, sizeof(dp));
}
int ans = 0;
for (int v = 0; v < MAX; v++) {
if (dp[3][v]) ans++;
}
return ans;
}
};
/**
* Problem: LeetCode 3514 - Number of Unique XOR Triplets II
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var uniqueXorTriplets = function(nums) {
const MAX = 2048;
let dp = Array.from({ length: 4 }, () => new Uint8Array(MAX));
dp[0][0] = 1;
for (const x of nums) {
const next = Array.from({ length: 4 }, (v, i) => new Uint8Array(dp[i]));
// Use current element up to 3 times (i <= j <= k allows repeats)
for (let t = 0; t < 3; t++) {
for (let k = 2; k >= 0; k--) {
for (let v = 0; v < MAX; v++) {
if (next[k][v]) {
next[k + 1][v ^ x] = 1;
}
}
}
}
dp = next;
}
let ans = 0;
for (let v = 0; v < MAX; v++) {
if (dp[3][v]) ans++;
}
return ans;
};