---
# System prepended metadata

title: 2588. Count the Number of Beautiful Subarrays
tags: [Leetcode, Bit Manipulation, XOR, Prefix Sum, Medium]

---

# 2588. Count the Number of Beautiful Subarrays
###### tags: `Leetcode` `Medium` `Prefix Sum` `Bit Manipulation` `XOR`
Link: https://leetcode.com/problems/count-the-number-of-beautiful-subarrays/description/
## 思路
如果我们统计每个bit里 1出现的次数 满足条件的subarray需要满足每个次数都是偶数
进一步 也就是这个subarray所有数字xor在一起 结果是0
## Code
```python=
class Solution:
    def beautifulSubarrays(self, nums: List[int]) -> int:
        freq = Counter({0:1})
        ans = prefix = 0
        for num in nums:
            prefix ^= num
            ans += freq[prefix]
            freq[prefix] += 1
        return ans
```