2588. Count the Number of Beautiful Subarrays
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 52% Topics: Array, Hash Table, Bit Manipulation, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
import collections
# freq table, combinatorics
class Solution(object):
def beautifulSubarrays(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt = collections.Counter()
cnt[0] = 1
result = curr = 0
for x in nums:
curr ^= x
result += cnt[curr]
cnt[curr] += 1
return result
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions