> ## 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.

# Regions Cut By Slashes Python Solution

> Tested Python solution for LeetCode 959 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 959, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Union Find](/catalog/topics/union-find), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/regions-cut-by-slashes/description/).

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

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

## Problem

An `n x n` grid is composed of `1 x 1` squares where each `1 x 1` square consists of a `'/`', `'\`', or blank space `' `'. These characters divide the square into contiguous regions.

Given the grid `grid` represented as a string array, return *the number of regions*.

Note that backslash characters are escaped, so a `'\`' is represented as `'\\`'.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2018/12/15/1.png)

```
Input: grid = [" /","/ "]
Output: 2
```

![Example 2](https://assets.leetcode.com/uploads/2018/12/15/2.png)

```
Input: grid = [" /","  "]
Output: 1
```

![Example 3](https://assets.leetcode.com/uploads/2018/12/15/4.png)

```
Input: grid = ["/\\","\\/"]
Output: 5
Explanation: Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.
```

### Constraints

* `n == grid.length == grid[i].length`
* `1 <= n <= 30`
* `grid[i][j]` is either `'/`', `'\`', or `' `'.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2 * alpha(n^2))
    # Space: O(n^2)
    def regions_by_slashes(self, grid: list[str]) -> int:
        n = len(grid)
        parent = list(range(4 * n * n))

        def find(x: int) -> int:
            while parent[x] != x:
                parent[x] = parent[parent[x]]
                x = parent[x]
            return x

        def union(a: int, b: int) -> bool:
            ra, rb = find(a), find(b)
            if ra == rb:
                return False
            parent[ra] = rb
            return True

        def node(i: int, j: int, part: int) -> int:
            return 4 * (i * n + j) + part

        regions = 4 * n * n
        for i, row in enumerate(grid):
            for j, char in enumerate(row):
                if char == "/":
                    regions -= int(union(node(i, j, 0), node(i, j, 3)))
                    regions -= int(union(node(i, j, 1), node(i, j, 2)))
                elif char == "\\":
                    regions -= int(union(node(i, j, 0), node(i, j, 1)))
                    regions -= int(union(node(i, j, 2), node(i, j, 3)))
                else:
                    regions -= int(union(node(i, j, 0), node(i, j, 1)))
                    regions -= int(union(node(i, j, 1), node(i, j, 2)))
                    regions -= int(union(node(i, j, 2), node(i, j, 3)))
                if j + 1 < n:
                    regions -= int(union(node(i, j, 1), node(i, j + 1, 3)))
                if i + 1 < n:
                    regions -= int(union(node(i, j, 2), node(i + 1, j, 0)))
        return regions
```

## Complexity

| Time | Space |
| - | - |
| O(n^2 \* alpha(n^2)) | O(n^2) |

## Tags

[NeetCode All](/catalog/neetcode).


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