diff --git a/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README.md b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README.md new file mode 100644 index 0000000000000..ebb4b15a1117e --- /dev/null +++ b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README.md @@ -0,0 +1,233 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README.md +--- + + + +# [3215. Count Triplets with Even XOR Set Bits II 🔒](https://leetcode.cn/problems/count-triplets-with-even-xor-set-bits-ii) + +[English Version](/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README_EN.md) + +## 题目描述 + + + +Given three integer arrays a, b, and c, return the number of triplets (a[i], b[j], c[k]), such that the bitwise XOR between the elements of each triplet has an even number of set bits. + +

 

+

Example 1:

+ +
+

Input: a = [1], b = [2], c = [3]

+ +

Output: 1

+ +

Explanation:

+ +

The only triplet is (a[0], b[0], c[0]) and their XOR is: 1 XOR 2 XOR 3 = 002.

+
+ +

Example 2:

+ +
+

Input: a = [1,1], b = [2,3], c = [1,5]

+ +

Output: 4

+ +

Explanation:

+ +

Consider these four triplets:

+ + +
+ +

 

+

Constraints:

+ + + + + +## 解法 + + + +### 方法一:位运算 + +对于两个整数,异或结果中 $1$ 的个数的奇偶性,取决于两个整数的二进制表示中 $1$ 的个数的奇偶性。 + +我们可以用三个数组 `cnt1`、`cnt2`、`cnt3` 分别记录数组 `a`、`b`、`c` 中每个数的二进制表示中 $1$ 的个数的奇偶性。 + +然后我们在 $[0, 1]$ 的范围内枚举三个数组中的每个数的二进制表示中 $1$ 的个数的奇偶性,如果三个数的二进制表示中 $1$ 的个数的奇偶性之和为偶数,那么这三个数的异或结果中 $1$ 的个数也为偶数,此时我们将这三个数的组合数相乘累加到答案中。 + +最后返回答案即可。 + +时间复杂度 $O(n)$,其中 $n$ 为数组 `a`、`b`、`c` 的长度。空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def tripletCount(self, a: List[int], b: List[int], c: List[int]) -> int: + cnt1 = Counter(x.bit_count() & 1 for x in a) + cnt2 = Counter(x.bit_count() & 1 for x in b) + cnt3 = Counter(x.bit_count() & 1 for x in c) + ans = 0 + for i in range(2): + for j in range(2): + for k in range(2): + if (i + j + k) & 1 ^ 1: + ans += cnt1[i] * cnt2[j] * cnt3[k] + return ans +``` + +#### Java + +```java +class Solution { + public long tripletCount(int[] a, int[] b, int[] c) { + int[] cnt1 = new int[2]; + int[] cnt2 = new int[2]; + int[] cnt3 = new int[2]; + for (int x : a) { + ++cnt1[Integer.bitCount(x) & 1]; + } + for (int x : b) { + ++cnt2[Integer.bitCount(x) & 1]; + } + for (int x : c) { + ++cnt3[Integer.bitCount(x) & 1]; + } + long ans = 0; + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { + for (int k = 0; k < 2; ++k) { + if ((i + j + k) % 2 == 0) { + ans += 1L * cnt1[i] * cnt2[j] * cnt3[k]; + } + } + } + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + long long tripletCount(vector& a, vector& b, vector& c) { + int cnt1[2]{}; + int cnt2[2]{}; + int cnt3[2]{}; + for (int x : a) { + ++cnt1[__builtin_popcount(x) & 1]; + } + for (int x : b) { + ++cnt2[__builtin_popcount(x) & 1]; + } + for (int x : c) { + ++cnt3[__builtin_popcount(x) & 1]; + } + long long ans = 0; + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { + for (int k = 0; k < 2; ++k) { + if ((i + j + k) % 2 == 0) { + ans += 1LL * cnt1[i] * cnt2[j] * cnt3[k]; + } + } + } + } + return ans; + } +}; +``` + +#### Go + +```go +func tripletCount(a []int, b []int, c []int) (ans int64) { + cnt1 := [2]int{} + cnt2 := [2]int{} + cnt3 := [2]int{} + for _, x := range a { + cnt1[bits.OnesCount(uint(x))%2]++ + } + for _, x := range b { + cnt2[bits.OnesCount(uint(x))%2]++ + } + for _, x := range c { + cnt3[bits.OnesCount(uint(x))%2]++ + } + for i := 0; i < 2; i++ { + for j := 0; j < 2; j++ { + for k := 0; k < 2; k++ { + if (i+j+k)%2 == 0 { + ans += int64(cnt1[i] * cnt2[j] * cnt3[k]) + } + } + } + } + return +} +``` + +#### TypeScript + +```ts +function tripletCount(a: number[], b: number[], c: number[]): number { + const cnt1: [number, number] = [0, 0]; + const cnt2: [number, number] = [0, 0]; + const cnt3: [number, number] = [0, 0]; + for (const x of a) { + ++cnt1[bitCount(x) & 1]; + } + for (const x of b) { + ++cnt2[bitCount(x) & 1]; + } + for (const x of c) { + ++cnt3[bitCount(x) & 1]; + } + let ans = 0; + for (let i = 0; i < 2; ++i) { + for (let j = 0; j < 2; ++j) { + for (let k = 0; k < 2; ++k) { + if ((i + j + k) % 2 === 0) { + ans += cnt1[i] * cnt2[j] * cnt3[k]; + } + } + } + } + return ans; +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} +``` + + + + + + diff --git a/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README_EN.md b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README_EN.md new file mode 100644 index 0000000000000..ad7e28c68dbb9 --- /dev/null +++ b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/README_EN.md @@ -0,0 +1,233 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README_EN.md +--- + + + +# [3215. Count Triplets with Even XOR Set Bits II 🔒](https://leetcode.com/problems/count-triplets-with-even-xor-set-bits-ii) + +[中文文档](/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README.md) + +## Description + + + +Given three integer arrays a, b, and c, return the number of triplets (a[i], b[j], c[k]), such that the bitwise XOR between the elements of each triplet has an even number of set bits. + +

 

+

Example 1:

+ +
+

Input: a = [1], b = [2], c = [3]

+ +

Output: 1

+ +

Explanation:

+ +

The only triplet is (a[0], b[0], c[0]) and their XOR is: 1 XOR 2 XOR 3 = 002.

+
+ +

Example 2:

+ +
+

Input: a = [1,1], b = [2,3], c = [1,5]

+ +

Output: 4

+ +

Explanation:

+ +

Consider these four triplets:

+ +
    +
  • (a[0], b[1], c[0]): 1 XOR 3 XOR 1 = 0112
  • +
  • (a[1], b[1], c[0]): 1 XOR 3 XOR 1 = 0112
  • +
  • (a[0], b[0], c[1]): 1 XOR 2 XOR 5 = 1102
  • +
  • (a[1], b[0], c[1]): 1 XOR 2 XOR 5 = 1102
  • +
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= a.length, b.length, c.length <= 105
  • +
  • 0 <= a[i], b[i], c[i] <= 109
  • +
+ + + +## Solutions + + + +### Solution 1: Bit Manipulation + +For two integers, the parity of the number of $1$s in the XOR result depends on the parity of the number of $1$s in the binary representations of the two integers. + +We can use three arrays `cnt1`, `cnt2`, `cnt3` to record the parity of the number of $1$s in the binary representations of each number in arrays `a`, `b`, `c`, respectively. + +Then, we enumerate the parity of the number of $1$s in the binary representations of each number in the three arrays within the range $[0, 1]$. If the sum of the parity of the number of $1$s in the binary representations of three numbers is even, then the number of $1$s in the XOR result of these three numbers is also even. At this time, we multiply the combination of these three numbers and accumulate it into the answer. + +Finally, return the answer. + +The time complexity is $O(n)$, where $n$ is the length of arrays `a`, `b`, `c`. The space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def tripletCount(self, a: List[int], b: List[int], c: List[int]) -> int: + cnt1 = Counter(x.bit_count() & 1 for x in a) + cnt2 = Counter(x.bit_count() & 1 for x in b) + cnt3 = Counter(x.bit_count() & 1 for x in c) + ans = 0 + for i in range(2): + for j in range(2): + for k in range(2): + if (i + j + k) & 1 ^ 1: + ans += cnt1[i] * cnt2[j] * cnt3[k] + return ans +``` + +#### Java + +```java +class Solution { + public long tripletCount(int[] a, int[] b, int[] c) { + int[] cnt1 = new int[2]; + int[] cnt2 = new int[2]; + int[] cnt3 = new int[2]; + for (int x : a) { + ++cnt1[Integer.bitCount(x) & 1]; + } + for (int x : b) { + ++cnt2[Integer.bitCount(x) & 1]; + } + for (int x : c) { + ++cnt3[Integer.bitCount(x) & 1]; + } + long ans = 0; + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { + for (int k = 0; k < 2; ++k) { + if ((i + j + k) % 2 == 0) { + ans += 1L * cnt1[i] * cnt2[j] * cnt3[k]; + } + } + } + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + long long tripletCount(vector& a, vector& b, vector& c) { + int cnt1[2]{}; + int cnt2[2]{}; + int cnt3[2]{}; + for (int x : a) { + ++cnt1[__builtin_popcount(x) & 1]; + } + for (int x : b) { + ++cnt2[__builtin_popcount(x) & 1]; + } + for (int x : c) { + ++cnt3[__builtin_popcount(x) & 1]; + } + long long ans = 0; + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { + for (int k = 0; k < 2; ++k) { + if ((i + j + k) % 2 == 0) { + ans += 1LL * cnt1[i] * cnt2[j] * cnt3[k]; + } + } + } + } + return ans; + } +}; +``` + +#### Go + +```go +func tripletCount(a []int, b []int, c []int) (ans int64) { + cnt1 := [2]int{} + cnt2 := [2]int{} + cnt3 := [2]int{} + for _, x := range a { + cnt1[bits.OnesCount(uint(x))%2]++ + } + for _, x := range b { + cnt2[bits.OnesCount(uint(x))%2]++ + } + for _, x := range c { + cnt3[bits.OnesCount(uint(x))%2]++ + } + for i := 0; i < 2; i++ { + for j := 0; j < 2; j++ { + for k := 0; k < 2; k++ { + if (i+j+k)%2 == 0 { + ans += int64(cnt1[i] * cnt2[j] * cnt3[k]) + } + } + } + } + return +} +``` + +#### TypeScript + +```ts +function tripletCount(a: number[], b: number[], c: number[]): number { + const cnt1: [number, number] = [0, 0]; + const cnt2: [number, number] = [0, 0]; + const cnt3: [number, number] = [0, 0]; + for (const x of a) { + ++cnt1[bitCount(x) & 1]; + } + for (const x of b) { + ++cnt2[bitCount(x) & 1]; + } + for (const x of c) { + ++cnt3[bitCount(x) & 1]; + } + let ans = 0; + for (let i = 0; i < 2; ++i) { + for (let j = 0; j < 2; ++j) { + for (let k = 0; k < 2; ++k) { + if ((i + j + k) % 2 === 0) { + ans += cnt1[i] * cnt2[j] * cnt3[k]; + } + } + } + } + return ans; +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} +``` + + + + + + diff --git a/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.cpp b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.cpp new file mode 100644 index 0000000000000..10effdfaf1976 --- /dev/null +++ b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + long long tripletCount(vector& a, vector& b, vector& c) { + int cnt1[2]{}; + int cnt2[2]{}; + int cnt3[2]{}; + for (int x : a) { + ++cnt1[__builtin_popcount(x) & 1]; + } + for (int x : b) { + ++cnt2[__builtin_popcount(x) & 1]; + } + for (int x : c) { + ++cnt3[__builtin_popcount(x) & 1]; + } + long long ans = 0; + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { + for (int k = 0; k < 2; ++k) { + if ((i + j + k) % 2 == 0) { + ans += 1LL * cnt1[i] * cnt2[j] * cnt3[k]; + } + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.go b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.go new file mode 100644 index 0000000000000..4efa959aad086 --- /dev/null +++ b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.go @@ -0,0 +1,24 @@ +func tripletCount(a []int, b []int, c []int) (ans int64) { + cnt1 := [2]int{} + cnt2 := [2]int{} + cnt3 := [2]int{} + for _, x := range a { + cnt1[bits.OnesCount(uint(x))%2]++ + } + for _, x := range b { + cnt2[bits.OnesCount(uint(x))%2]++ + } + for _, x := range c { + cnt3[bits.OnesCount(uint(x))%2]++ + } + for i := 0; i < 2; i++ { + for j := 0; j < 2; j++ { + for k := 0; k < 2; k++ { + if (i+j+k)%2 == 0 { + ans += int64(cnt1[i] * cnt2[j] * cnt3[k]) + } + } + } + } + return +} \ No newline at end of file diff --git a/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.java b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.java new file mode 100644 index 0000000000000..1dfb5b152c3e4 --- /dev/null +++ b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.java @@ -0,0 +1,27 @@ +class Solution { + public long tripletCount(int[] a, int[] b, int[] c) { + int[] cnt1 = new int[2]; + int[] cnt2 = new int[2]; + int[] cnt3 = new int[2]; + for (int x : a) { + ++cnt1[Integer.bitCount(x) & 1]; + } + for (int x : b) { + ++cnt2[Integer.bitCount(x) & 1]; + } + for (int x : c) { + ++cnt3[Integer.bitCount(x) & 1]; + } + long ans = 0; + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { + for (int k = 0; k < 2; ++k) { + if ((i + j + k) % 2 == 0) { + ans += 1L * cnt1[i] * cnt2[j] * cnt3[k]; + } + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.py b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.py new file mode 100644 index 0000000000000..f9dd29bb53e3f --- /dev/null +++ b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def tripletCount(self, a: List[int], b: List[int], c: List[int]) -> int: + cnt1 = Counter(x.bit_count() & 1 for x in a) + cnt2 = Counter(x.bit_count() & 1 for x in b) + cnt3 = Counter(x.bit_count() & 1 for x in c) + ans = 0 + for i in range(2): + for j in range(2): + for k in range(2): + if (i + j + k) & 1 ^ 1: + ans += cnt1[i] * cnt2[j] * cnt3[k] + return ans diff --git a/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.ts b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.ts new file mode 100644 index 0000000000000..05376ee4c92be --- /dev/null +++ b/solution/3200-3299/3215.Count Triplets with Even XOR Set Bits II/Solution.ts @@ -0,0 +1,34 @@ +function tripletCount(a: number[], b: number[], c: number[]): number { + const cnt1: [number, number] = [0, 0]; + const cnt2: [number, number] = [0, 0]; + const cnt3: [number, number] = [0, 0]; + for (const x of a) { + ++cnt1[bitCount(x) & 1]; + } + for (const x of b) { + ++cnt2[bitCount(x) & 1]; + } + for (const x of c) { + ++cnt3[bitCount(x) & 1]; + } + let ans = 0; + for (let i = 0; i < 2; ++i) { + for (let j = 0; j < 2; ++j) { + for (let k = 0; k < 2; ++k) { + if ((i + j + k) % 2 === 0) { + ans += cnt1[i] * cnt2[j] * cnt3[k]; + } + } + } + } + return ans; +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} diff --git a/solution/README.md b/solution/README.md index d8b5ab68000ab..d5698ff7fc79c 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3225,6 +3225,7 @@ | 3212 | [统计 X 和 Y 频数相等的子矩阵数量](/solution/3200-3299/3212.Count%20Submatrices%20With%20Equal%20Frequency%20of%20X%20and%20Y/README.md) | `数组`,`矩阵`,`前缀和` | 中等 | 第 405 场周赛 | | 3213 | [最小代价构造字符串](/solution/3200-3299/3213.Construct%20String%20with%20Minimum%20Cost/README.md) | `数组`,`字符串`,`动态规划`,`后缀数组` | 困难 | 第 405 场周赛 | | 3214 | [同比增长率](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) | `数据库` | 困难 | 🔒 | +| 3215 | [Count Triplets with Even XOR Set Bits II](/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index b4e4e0f9e97cb..82525662631a8 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3223,6 +3223,7 @@ Press Control + F(or Command + F on | 3212 | [Count Submatrices With Equal Frequency of X and Y](/solution/3200-3299/3212.Count%20Submatrices%20With%20Equal%20Frequency%20of%20X%20and%20Y/README_EN.md) | `Array`,`Matrix`,`Prefix Sum` | Medium | Weekly Contest 405 | | 3213 | [Construct String with Minimum Cost](/solution/3200-3299/3213.Construct%20String%20with%20Minimum%20Cost/README_EN.md) | `Array`,`String`,`Dynamic Programming`,`Suffix Array` | Hard | Weekly Contest 405 | | 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md) | `Database` | Hard | 🔒 | +| 3215 | [Count Triplets with Even XOR Set Bits II](/solution/3200-3299/3215.Count%20Triplets%20with%20Even%20XOR%20Set%20Bits%20II/README_EN.md) | | Medium | 🔒 | ## Copyright