diff --git a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README.md b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README.md index 6ab254a66ef70..277503395e08d 100644 --- a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README.md +++ b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README.md @@ -85,7 +85,13 @@ tags: -### 方法一 +### 方法一:枚举 + +根据题目描述,要找到满足 $a = b$ 的三元组 $(i, j, k)$,即满足 $s = a \oplus b = 0$,我们只需要枚举左端点 $i$,然后计算以 $k$ 为右端点的区间 $[i, k]$ 的前缀异或和 $s$,如果 $s = 0$,那么对于任意 $j \in [i + 1, k]$,都满足 $a = b$,即 $(i, j, k)$ 是一个满足条件的三元组,一共有 $k - i$ 个,我们将其累加到答案中即可。 + +枚举结束后,返回答案即可。 + +时间复杂度 $O(n^2)$,其中 $n$ 是数组 $\textit{arr}$ 的长度。空间复杂度 $O(1)$。 @@ -94,17 +100,13 @@ tags: ```python class Solution: def countTriplets(self, arr: List[int]) -> int: - n = len(arr) - pre = [0] * (n + 1) - for i in range(n): - pre[i + 1] = pre[i] ^ arr[i] - ans = 0 - for i in range(n - 1): - for j in range(i + 1, n): - for k in range(j, n): - a, b = pre[j] ^ pre[i], pre[k + 1] ^ pre[j] - if a == b: - ans += 1 + ans, n = 0, len(arr) + for i, x in enumerate(arr): + s = x + for k in range(i + 1, n): + s ^= arr[k] + if s == 0: + ans += k - i return ans ``` @@ -113,20 +115,13 @@ class Solution: ```java class Solution { public int countTriplets(int[] arr) { - int n = arr.length; - int[] pre = new int[n + 1]; + int ans = 0, n = arr.length; for (int i = 0; i < n; ++i) { - pre[i + 1] = pre[i] ^ arr[i]; - } - int ans = 0; - for (int i = 0; i < n - 1; ++i) { - for (int j = i + 1; j < n; ++j) { - for (int k = j; k < n; ++k) { - int a = pre[j] ^ pre[i]; - int b = pre[k + 1] ^ pre[j]; - if (a == b) { - ++ans; - } + int s = arr[i]; + for (int k = i + 1; k < n; ++k) { + s ^= arr[k]; + if (s == 0) { + ans += k - i; } } } @@ -141,15 +136,13 @@ class Solution { class Solution { public: int countTriplets(vector& arr) { - int n = arr.size(); - vector pre(n + 1); - for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i]; - int ans = 0; - for (int i = 0; i < n - 1; ++i) { - for (int j = i + 1; j < n; ++j) { - for (int k = j; k < n; ++k) { - int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j]; - if (a == b) ++ans; + int ans = 0, n = arr.size(); + for (int i = 0; i < n; ++i) { + int s = arr[i]; + for (int k = i + 1; k < n; ++k) { + s ^= arr[k]; + if (s == 0) { + ans += k - i; } } } @@ -161,24 +154,59 @@ public: #### Go ```go -func countTriplets(arr []int) int { - n := len(arr) - pre := make([]int, n+1) - for i := 0; i < n; i++ { - pre[i+1] = pre[i] ^ arr[i] - } - ans := 0 - for i := 0; i < n-1; i++ { - for j := i + 1; j < n; j++ { - for k := j; k < n; k++ { - a, b := pre[j]^pre[i], pre[k+1]^pre[j] - if a == b { - ans++ - } +func countTriplets(arr []int) (ans int) { + for i, x := range arr { + s := x + for k := i + 1; k < len(arr); k++ { + s ^= arr[k] + if s == 0 { + ans += k - i } } } - return ans + return +} +``` + +#### TypeScript + +```ts +function countTriplets(arr: number[]): number { + const n = arr.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + let s = arr[i]; + for (let k = i + 1; k < n; ++k) { + s ^= arr[k]; + if (s === 0) { + ans += k - i; + } + } + } + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn count_triplets(arr: Vec) -> i32 { + let mut ans = 0; + let n = arr.len(); + + for i in 0..n { + let mut s = arr[i]; + for k in (i + 1)..n { + s ^= arr[k]; + if s == 0 { + ans += (k - i) as i32; + } + } + } + + ans + } } ``` diff --git a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README_EN.md b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README_EN.md index 848df5d85c8d1..c7365a33d2784 100644 --- a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README_EN.md +++ b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/README_EN.md @@ -67,7 +67,13 @@ tags: -### Solution 1 +### Solution 1: Enumeration + +According to the problem description, to find triplets $(i, j, k)$ that satisfy $a = b$, which means $s = a \oplus b = 0$, we only need to enumerate the left endpoint $i$, and then calculate the prefix XOR sum $s$ of the interval $[i, k]$ with $k$ as the right endpoint. If $s = 0$, then for any $j \in [i + 1, k]$, the condition $a = b$ is satisfied, meaning $(i, j, k)$ is a valid triplet. There are $k - i$ such triplets, which we can add to our answer. + +After the enumeration is complete, we return the answer. + +The time complexity is $O(n^2)$, where $n$ is the length of the array $\textit{arr}$. The space complexity is $O(1)$. @@ -76,17 +82,13 @@ tags: ```python class Solution: def countTriplets(self, arr: List[int]) -> int: - n = len(arr) - pre = [0] * (n + 1) - for i in range(n): - pre[i + 1] = pre[i] ^ arr[i] - ans = 0 - for i in range(n - 1): - for j in range(i + 1, n): - for k in range(j, n): - a, b = pre[j] ^ pre[i], pre[k + 1] ^ pre[j] - if a == b: - ans += 1 + ans, n = 0, len(arr) + for i, x in enumerate(arr): + s = x + for k in range(i + 1, n): + s ^= arr[k] + if s == 0: + ans += k - i return ans ``` @@ -95,20 +97,13 @@ class Solution: ```java class Solution { public int countTriplets(int[] arr) { - int n = arr.length; - int[] pre = new int[n + 1]; + int ans = 0, n = arr.length; for (int i = 0; i < n; ++i) { - pre[i + 1] = pre[i] ^ arr[i]; - } - int ans = 0; - for (int i = 0; i < n - 1; ++i) { - for (int j = i + 1; j < n; ++j) { - for (int k = j; k < n; ++k) { - int a = pre[j] ^ pre[i]; - int b = pre[k + 1] ^ pre[j]; - if (a == b) { - ++ans; - } + int s = arr[i]; + for (int k = i + 1; k < n; ++k) { + s ^= arr[k]; + if (s == 0) { + ans += k - i; } } } @@ -123,15 +118,13 @@ class Solution { class Solution { public: int countTriplets(vector& arr) { - int n = arr.size(); - vector pre(n + 1); - for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i]; - int ans = 0; - for (int i = 0; i < n - 1; ++i) { - for (int j = i + 1; j < n; ++j) { - for (int k = j; k < n; ++k) { - int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j]; - if (a == b) ++ans; + int ans = 0, n = arr.size(); + for (int i = 0; i < n; ++i) { + int s = arr[i]; + for (int k = i + 1; k < n; ++k) { + s ^= arr[k]; + if (s == 0) { + ans += k - i; } } } @@ -143,24 +136,59 @@ public: #### Go ```go -func countTriplets(arr []int) int { - n := len(arr) - pre := make([]int, n+1) - for i := 0; i < n; i++ { - pre[i+1] = pre[i] ^ arr[i] - } - ans := 0 - for i := 0; i < n-1; i++ { - for j := i + 1; j < n; j++ { - for k := j; k < n; k++ { - a, b := pre[j]^pre[i], pre[k+1]^pre[j] - if a == b { - ans++ - } +func countTriplets(arr []int) (ans int) { + for i, x := range arr { + s := x + for k := i + 1; k < len(arr); k++ { + s ^= arr[k] + if s == 0 { + ans += k - i } } } - return ans + return +} +``` + +#### TypeScript + +```ts +function countTriplets(arr: number[]): number { + const n = arr.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + let s = arr[i]; + for (let k = i + 1; k < n; ++k) { + s ^= arr[k]; + if (s === 0) { + ans += k - i; + } + } + } + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn count_triplets(arr: Vec) -> i32 { + let mut ans = 0; + let n = arr.len(); + + for i in 0..n { + let mut s = arr[i]; + for k in (i + 1)..n { + s ^= arr[k]; + if s == 0 { + ans += (k - i) as i32; + } + } + } + + ans + } } ``` diff --git a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.cpp b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.cpp index 2fbe1327ff539..824af81230fe8 100644 --- a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.cpp +++ b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.cpp @@ -1,18 +1,16 @@ class Solution { public: int countTriplets(vector& arr) { - int n = arr.size(); - vector pre(n + 1); - for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i]; - int ans = 0; - for (int i = 0; i < n - 1; ++i) { - for (int j = i + 1; j < n; ++j) { - for (int k = j; k < n; ++k) { - int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j]; - if (a == b) ++ans; + int ans = 0, n = arr.size(); + for (int i = 0; i < n; ++i) { + int s = arr[i]; + for (int k = i + 1; k < n; ++k) { + s ^= arr[k]; + if (s == 0) { + ans += k - i; } } } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.go b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.go index bc1822e5bd6b8..8b3b99077232c 100644 --- a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.go +++ b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.go @@ -1,19 +1,12 @@ -func countTriplets(arr []int) int { - n := len(arr) - pre := make([]int, n+1) - for i := 0; i < n; i++ { - pre[i+1] = pre[i] ^ arr[i] - } - ans := 0 - for i := 0; i < n-1; i++ { - for j := i + 1; j < n; j++ { - for k := j; k < n; k++ { - a, b := pre[j]^pre[i], pre[k+1]^pre[j] - if a == b { - ans++ - } +func countTriplets(arr []int) (ans int) { + for i, x := range arr { + s := x + for k := i + 1; k < len(arr); k++ { + s ^= arr[k] + if s == 0 { + ans += k - i } } } - return ans -} \ No newline at end of file + return +} diff --git a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.java b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.java index db3c8e1e270ed..0f681ef434a54 100644 --- a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.java +++ b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.java @@ -1,22 +1,15 @@ class Solution { public int countTriplets(int[] arr) { - int n = arr.length; - int[] pre = new int[n + 1]; + int ans = 0, n = arr.length; for (int i = 0; i < n; ++i) { - pre[i + 1] = pre[i] ^ arr[i]; - } - int ans = 0; - for (int i = 0; i < n - 1; ++i) { - for (int j = i + 1; j < n; ++j) { - for (int k = j; k < n; ++k) { - int a = pre[j] ^ pre[i]; - int b = pre[k + 1] ^ pre[j]; - if (a == b) { - ++ans; - } + int s = arr[i]; + for (int k = i + 1; k < n; ++k) { + s ^= arr[k]; + if (s == 0) { + ans += k - i; } } } return ans; } -} \ No newline at end of file +} diff --git a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.py b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.py index bb0b701eb1e2c..859ffb09a8882 100644 --- a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.py +++ b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.py @@ -1,14 +1,10 @@ class Solution: def countTriplets(self, arr: List[int]) -> int: - n = len(arr) - pre = [0] * (n + 1) - for i in range(n): - pre[i + 1] = pre[i] ^ arr[i] - ans = 0 - for i in range(n - 1): - for j in range(i + 1, n): - for k in range(j, n): - a, b = pre[j] ^ pre[i], pre[k + 1] ^ pre[j] - if a == b: - ans += 1 + ans, n = 0, len(arr) + for i, x in enumerate(arr): + s = x + for k in range(i + 1, n): + s ^= arr[k] + if s == 0: + ans += k - i return ans diff --git a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.rs b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.rs new file mode 100644 index 0000000000000..9aaacf67018ff --- /dev/null +++ b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.rs @@ -0,0 +1,18 @@ +impl Solution { + pub fn count_triplets(arr: Vec) -> i32 { + let mut ans = 0; + let n = arr.len(); + + for i in 0..n { + let mut s = arr[i]; + for k in (i + 1)..n { + s ^= arr[k]; + if s == 0 { + ans += (k - i) as i32; + } + } + } + + ans + } +} diff --git a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.ts b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.ts new file mode 100644 index 0000000000000..1a2db2a72b816 --- /dev/null +++ b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.ts @@ -0,0 +1,14 @@ +function countTriplets(arr: number[]): number { + const n = arr.length; + let ans = 0; + for (let i = 0; i < n; ++i) { + let s = arr[i]; + for (let k = i + 1; k < n; ++k) { + s ^= arr[k]; + if (s === 0) { + ans += k - i; + } + } + } + return ans; +}