Regions Cut By Slashes

Given an n x n grid containing forward and back slash characters, return the number of regions formed after drawing the slashes. A common DSU approach is to split each cell into four triangles and detect cycles when triangles are already connected.

Input Format

grid = array of slash strings

Output Format

number of regions formed by the slashes

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

grid = [" /","/ "]

Output:

2

Explanation:

The two slashes divide the square into two regions.

Example 2:

Input:

grid = [" /","  "]

Output:

1

Explanation:

Only one slash exists, so there is a single connected region.

Example 3:

Input:

grid = ["\\/","/\\"]

Output:

4

Explanation:

The crossing slash layout creates multiple enclosed regions.

Loading...
Regions Cut By Slashes - Union Find DSA Problem