<p>An <code>n x n</code> grid is composed of <code>1 x 1</code> squares where each <code>1 x 1</code> square consists of a <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or blank space <code>&#39; &#39;</code>. These characters divide the square into contiguous regions.</p>

<p>Given the grid <code>grid</code> represented as a string array, return <em>the number of regions</em>.</p>

<p>Note that backslash characters are escaped, so a <code>&#39;\&#39;</code> is represented as <code>&#39;\\&#39;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/1.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;/ &quot;]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/2.png" style="width: 200px; height: 198px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;  &quot;]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/4.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot;/\\&quot;,&quot;\\/&quot;]
<strong>Output:</strong> 5
<strong>Explanation: </strong>Recall that because \ characters are escaped, &quot;\\/&quot; refers to \/, and &quot;/\\&quot; refers to /\.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 30</code></li>
	<li><code>grid[i][j]</code> is either <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or <code>&#39; &#39;</code>.</li>
</ul>

class Solution:
    def regionsBySlashes(self, grid: List[str]) -> int:
        
        def find(x):
            nonlocal parents

            if parents[x] != x:
                parents[x] = find(parents[x])

            return parents[x]

        def union(x, y):
            nonlocal parents, ranks, regions

            parent_x, parent_y = find(x), find(y)
            if parent_x == parent_y:
                return

            if ranks[parent_x] > ranks[parent_y]:
                parents[parent_y] = parent_x
            elif ranks[parent_x] < ranks[parent_y]:
                parents[parent_x] = parent_y
            else:
                parents[parent_y] = parent_x
                ranks[parent_x] += 1

            regions -= 1

            return

        n = len(grid)

        sections = {"U": ((-1, 0), "D"), "D": ((1, 0), "U"), "L": ((0, -1), "R"), "R": ((0, 1), "L")}
        parents = {(i, j, s): (i, j, s) for i in range(n) for j in range(n) for s in sections}
        ranks = {(i, j, s): 0 for i in range(n) for j in range(n) for s in sections}
        regions = n * n * 4

        for i in range(n):
            for j in range(n):
                for s in sections:
                    # inner union
                    if grid[i][j] == "/":
                        union((i, j, "U"), (i, j, "L"))
                        union((i, j, "D"), (i, j, "R"))
                    elif grid[i][j] == "\\":
                        union((i, j, "U"), (i, j, "R"))
                        union((i, j, "D"), (i, j, "L"))
                    else:
                        union((i, j, "U"), (i, j, "R"))
                        union((i, j, "D"), (i, j, "L"))
                        union((i, j, "U"), (i, j, "D"))

                    # outer union
                    offset, outer_s = sections[s]
                    outer_i, outer_j = i + offset[0], j + offset[1]
                    if (0 <= outer_i < n) and (0 <= outer_j < n):
                        union((i, j, s), (outer_i, outer_j, outer_s))
        
        return regions
24410