> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Binary Subarrays With Sum Python Solution

> Tested Python solution for LeetCode 930 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 930, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Sliding Window](/catalog/topics/sliding-window), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/binary-subarrays-with-sum/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 930   # by problem number
lcpy gen -s binary_subarrays_with_sum   # by problem name
```

## Problem

\<p>Given a binary array \<code>nums\</code> and an integer \<code>goal\</code>, return \<em>the number of non-empty \<strong>subarrays\</strong> with a sum\</em> \<code>goal\</code>.\</p>

\<p>A \<strong>subarray\</b> is a contiguous part of the array.\</p>

### Examples

```
Input: nums = [1,0,1,0,1], goal = 2
Output: 4
Explanation: The 4 subarrays are bolded and underlined below:
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
```

```
Input: nums = [0,0,0,0,0], goal = 0
Output: 15
```

### Constraints

* 1 \<= nums.length \<= 3 \* 10^4
* nums\[i] is either 0 or 1.
* 0 \<= goal \<= nums.length

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_subarrays_with_sum/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_subarrays_with_sum/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def num_subarrays_with_sum(self, nums: list[int], goal: int) -> int:
        # Prefix sum counts: sum -> number of prefixes with that sum
        prefix_counts: dict[int, int] = {0: 1}
        total = 0
        count = 0
        for num in nums:
            total += num
            count += prefix_counts.get(total - goal, 0)
            prefix_counts[total] = prefix_counts.get(total, 0) + 1
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.